Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.

Similar presentations


Presentation on theme: "CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting."— Presentation transcript:

1 CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting Use of arrays to store collections of data Reading in Lewis/Loftus 6.1 (208-215)

2 CS100A, Fall 1997, Lecture 112 New Problem Input data: Zero or more exam grades in the range 0 to 100, followed by a –1. Requested output: A histogram showing, for each possible score, the number of students receiving that score. What is the general form of the solution? What must be done: Before reading any grades? Each time a grade is read? After all grades have been read? How many variables are needed to keep track of the histogram data? grade frequency 0 0 1 2 … 99 8 100 3

3 CS100A, Fall 1997, Lecture 113 A Tedious Solution public static void main(String args[]) { int grade; // current grade int freq0; // # students with grade = 0 int freq1; // # students with grade = 1 … int freq98; // # students with grade = 98 int freq99; // # students with grade = 99 int freq100; // # students with grade = 100 // set histogram variables to 0 freq0 = 0; freq1 = 0; freq2 = 0; … freq98 = 0; freq99 = 0; freq100 = 0;

4 CS100A, Fall 1997, Lecture 114 A Tedious Solution (cont.) // read grades and record them in histogram grade = in.readInt( ); // inv: frequency of grades processed so far // recorded in freq0, …, freq100. while (grade != -1) { // record current grade in histogram if (grade == 0) freq0 = freq0 + 1; else if (grade == 1) freq1 = freq1 + 1; … else if (grade == 98) freq98 = freq98 + 1; else if (grade == 99) freq99 = freq99 + 1; else if (grade==100) freq100=freq100+1; // read next grade grade = in.readInt( ); }

5 CS100A, Fall 1997, Lecture 115 A Tedious Solution (cont.) // print histogram System.out.println(“grade frequency”); System.out.println( 0 + “ ” + freq0); System.out.println( 1 + “ ” + freq1); … System.out.println( 98 + “ ” + freq98); System.out.println( 99 + “ ” + freq99); System.out.println(100 + “ ”+freq100); }

6 CS100A, Fall 1997, Lecture 116 Arrays of Variables A much better solution is to use an array of integer variables to store the frequencies. The declaration int[ ] freq; declares a variable of type int[ ] (int array). It does not actually allocate the array. The assignment freq = new int[101]; allocates an array containing 101 int variables. Graphically 0 1 2 3 … 98 99 100 freq freq is called an array. The variable freq[expression] refers to one of the variables freq[0], freq[1], …, freq[100], depending on the value of expression.

7 CS100A, Fall 1997, Lecture 117 A Better Solution public static void main(String args[]) { int grade; // current grade int[ ] freq; // freq[i] is the number of // students receiving grade i int k; // allocate histogram array and initialize // entries to 0 freq = new int[101]; k = 0; // inv: freq[0..k-1] = 0 while (k <= 100) { freq[k] = 0; k = k + 1; }

8 CS100A, Fall 1997, Lecture 118 A Better Solution (cont) // read grades and record them in histogram grade = in.readInt( ); // inv: freq contains frequencies of grades // previously processed while (grade != -1) { // record current grade in histogram freq[grade] = freq[grade] + 1; // input next grade grade = in.readInt( ); } // print histogram System.out.println(“grade frequency”); k = 0; // inv: freq[0..k-1] has been printed while (k <= 100) { System.out.println( k + “ ” + freq[k]); k = k + 1; }

9 CS100A, Fall 1997, Lecture 119 Trace Test input data: 100 98 2 98 99 … –1 Variables: grade 0 1 2 3 … 98 99 100 freq k

10 CS100A, Fall 1997, Lecture 1110 Notation for Array Sections When talking about algorithms that use arrays, it is often convenient to be able to talk about a section of an array. We use the notation A[i..j] to refer to the section of the array containing the variables A[i], A[i+1], …, A[j-1], A[j] This is not Java notation — in a Java program, array elements may only be accessed one at a time with a single subscript. We use the convention that, if i > j, the array section A[i..j] is empty, i.e., it contains no elements and has length 0.

11 CS100A, Fall 1997, Lecture 1111 New Problem Input data (as before): Zero or more grades in the range 0 to 100, followed by a –1. Example: 98 85 93 22 89 92 –1 Requested output: The input grades printed in the reverse of the order they were read. Example: 92 89 22 93 85 98 The usual questions apply: What is the general form of the solution? What must be done: Before reading any grades? Each time a grade is read? After all grades have been read? How large an array is needed to hold the data?

12 CS100A, Fall 1997, Lecture 1112 Solution: Store Input in an Array We store the input in an integer array in the order it was read. Logically associated with the array is an integer variable that contains the number of grades stored so far. We also pick an arbitrary number, MaxGrades, as an upper limit on the number of grades that can be stored. 0 1 2 3 … MaxGrades grades nGrades Invariant (general rule): Grades are stored in grades[0..nGrades-1] with grades[0] being the first grade, grades[1] the second, etc. nGrades <= MaxGrades. If nGrades = MaxGrades, the array is “full” and no more data can be stored.

13 CS100A, Fall 1997, Lecture 1113 Solution // read a list of grades and print them in // reverse order public static void main(String args[]) { int MaxGrades = 10; // Maximum # grades // that can be stored // grades are stored in grades[0..nGrades-1] // with grades[0] being the first grade read, // grades[1] being the second, etc. int [ ] grades = new int[MaxGrades]; int nGrades; // # grades stored int grade; // current grade from input int k; // initialize list of grades to empty nGrades = 0;

14 CS100A, Fall 1997, Lecture 1114 Solution (cont.) // Read grades from input and store them. // inv: grades[0..nGrades-1] are the grades // read so far. grade = in.readInt(); while (grade != -1) { grades[nGrades] = grade; nGrades = nGrades + 1; grade = in.readInt(); } // print the grades in reverse order k = nGrades -1; // inv: grades[k+1..nGrades-1] has been // printed in reverse order while (k >= 0) { System.out.print(grades[k] + " "); k = k - 1; } System.out.println(); }

15 CS100A, Fall 1997, Lecture 1115 Trace Test input data: 97 93 95 -1 Variables 0 1 2 3 … MaxGrades grades nGrades grade k

16 CS100A, Fall 1997, Lecture 1116 Rules When an array is allocated int[ ] A = new int[size]; size may be any integer expression. When using A[exp] to refer to an element, the subscript exp may be any integer expression. The elements in an array are numbered A[0], A[1], …, A[size-1], where size is the number of elements in the array. In a reference to an array element A[exp], it is an error if exp<0 or expsize. Be sure to be careful to distinguish the subscript (or location) of an array element from the data that is actually stored in that location.

17 CS100A, Fall 1997, Lecture 1117 A Note for C/C++ Programmers In C or C++, the brackets in an array declaration are written after the variable name (which is most confusing), not as part of the type. Java also allows this, so the declaration int[ ] A = new int[size]; could have been written like this: int A[ ] = new int[size]; We will always use the first form to emphasize that the brackets, which indicate an array, are part of the type of the variable.


Download ppt "CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting."

Similar presentations


Ads by Google