Array of numbers, Array of strings COMP 102 - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Array of numbers, Array of strings COMP 102

Description:

See L&L 5.8. 21 :7. Using an Array: using for loops. Same as before, but with for loop: ... { table[ i ] = Math.sqrt(i); for ( int i = 1; i = 100; i ) ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 13
Provided by: pon9
Category:
Tags: comp | array | math | numbers | see | strings

less

Transcript and Presenter's Notes

Title: Array of numbers, Array of strings COMP 102


1
Array of numbers, Array of stringsCOMP 102 21
2008 T2
2
Outline
  • Using Arrays
  • loops for arrays
  • Exercises
  • Administrivia
  • A7 handout
  • Friday lecture
  • common problems in term test,
  • Array exercises
  • Pick up term test from School Office at CO358
  • Model solutions are online

3
Declaring and Creating Arrays
  • Type of an array
  • ?Type of element gt
  • To use an array
  • must declare a variable or field to hold the
    array
  • MovingShapes allShapes
  • int numbers
  • must create an empty array
  • allShapes new MovingShapes20
  • numbers new int 1000
  • must put values into the elements of the array
  • numbers0 5
  • numbers100 numbers6 numbers80

4
Initialising Arrays (LL p379)
  • Java has a special syntax for initialising
    arrays
  • int outOf 10, 10, 15, 8, 20
  • char vowels a, e, i, o, u
  • String courses "COMP102", "COMP103",
    "MATH114",
    SWEN102", "STAT131", ENGR102"
  • Square saver new Square(10, 10, 10,
    Color.blue), new
    Square(10, 10, 10, Color.red),
    new Square(10, 10, 10, Color.green)
  • String siblings
  • Can only be used in the declarations!

5
Using an Array loops
  • Usually, want to act on lots of elements
  • ? access and assign array elements inside a
    loop
  • Print out all the marks
  • int num 0
  • while (num lt marks.length)
  • System.out.printf( Student 3d got 4.1f, num,
    marks num )
  • num num 1
  • Compute final marks from essay marks and exam
    marks
  • int num 0
  • while (num lt marks.length)
  • marksnum essaynum0.6 examnum0.4
  • num num1

6
Using an Array for loops
  • Standard pattern for processing each element of
    an array
  • int i 0
  • while (i lt data.length )
  • data i
  • i i1
  • The for loop is a shorthand
  • for (int i 0 i lt data.length i i1)
  • data i
  • or
  • for (int i 0 i lt data.length i)
  • data i

See LL 5.8
7
Using an Array using for loops
  • Same as before, but with for loop
  • ? easier to read
  • Print out all the marks
  • for (int num 0 num lt marks.length num)
  • System.out.printf( Student 3d got 4.1f, num,
    marks num )
  • Compute final marks from essay marks and exam
    marks
  • for (int i 0 i lt marks.length i)
  • marks i essay i 0.6 exam i 0.4

8
Exercise
  • Write code to fill an array of numbers then print
    them out
  • The array should hold doubles.
  • The numbers should be the square roots of the
    integers from 1 to 100.
  • public void squareRootTable()
  • double table new double 110
  • for ( int i 1 i lt 100 i)
  • table i Math.sqrt(i)
  • for ( int i 1 i lt 100 i)
  • System.out.printf(Sqrt of d is 6.3f \n, i,
    table i )

9
Reading data from a file into an array
  • Suppose grades.txt contains student grades (in
    order of ID)
  • String grades new String 200
  • try
  • Scanner sc new Scanner (new File(grades.txt))
  • int n 0
  • while ( sc.hasNext() n lt grades.length)
  • gradesn sc.next()
  • n
  • catch (IOException e) System.out.printf( File
    failure s\n, e)

A C B B- A A-
dont go past end of array
dont go past end of file
10
Reading data from a file into an array
  • Suppose tutor1.txt contains student numbers and
    grades
  • String grades new String 200
  • try
  • Scanner sc new Scanner (new File(tutor1.txt))
  • while ( sc.hasNext() )
  • int id sc.nextInt()
  • String grade sc.next()
  • if ( 0ltid id lt grades.length)
  • gradesid grade
  • catch (IOException e) System.out.printf(
    File failure s\n, e)
  • 7 A
  • 3 C
  • 27 B
  • 154 B-
  • A
  • 103 A-

null
null
C
null
null
null
A
?
null
null
null
null
1
2
3
4
5
6
7
199
0
200
length
11
Arrays with meaningful indexes
  • For some arrays, the index represents a
    meaningful value
  • eg, the student number.
  • every element of the array is meaningful.
  • If no value has been assigned to an array
    element,then it will contain null (if an
    object) or 0 (if a number)
  • Be careful to check if element is empty before
    using it
  • Print out all the grades
  • for (int num 0 num lt grades.length num )
  • if ( gradesnum ! null )
  • System.out.printf( Student 3d got 4.1f,
  • num, grades num )

12
Exercise
  • Write a method that will read a sequence of stock
    prices from a file into an array and return the
    array.
  • public double readPrices(String fname)
  • double prices new double 200
  • try
  • Scanner sc new Scanner (new File(fname))
  • int count 0
  • while ( sc.hasNext() count lt 200)
  • prices count sc.nextDouble()
  • sc.close()
  • return prices
  • catch (IOException e) System.out.printf(
    File failure s\n, e)
Write a Comment
User Comments (0)
About PowerShow.com