Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Arrays

Similar presentations


Presentation on theme: "Introduction to Arrays"— Presentation transcript:

1 Introduction to Arrays
CS100A Lect. 11, 6 Oct. 1998 Introduction to Arrays Array declaration and allocation Subscripting Use of arrays to store collections of data Reading in Holmes for arrays: Chapter 5 (p. 147) We’ll spend several lectures using arrays. CS100A, Lecture 11, 6 October 1998

2 Data processing problem
Input: zero or more grades in the range , preceded by the number of grades Sample input: Task: Read grades and print information about them: Print grades in print grades in the reverse of the order given. e.g Print grades in increasing order. e.g Print histogram. Thoughts: 0. Need to read in all the values before doing any processing. 1. Need as many variables as elements of the array. 2. Impossible with knowledge of Java that we now have --what if there are hundreds of grades to deal with? Need a different variable for each one? CS100A, Lecture 11, 6 October 1998

3 The ARRAY helps solve the problem
g g[0] is 90 g[1] is g.length is the number of array g[2] is elements in array g g[3] is g.length = 5 g[4] is in “g[4]”, 4 is the “subscript” or “index” h.length is the number of array elements in array h h.length = 206 h CS100A, Lecture 11, 6 October 1998

4 Conventions g h i j k Represents 3 array segments or sections:
segment number of values in it empty when g[h..i-1] i-h h = i g[i..j] j+1-i j = i-1 g[j+1..k-1] k-(j+1) j = k h i j k CS100A, Lecture 11, 6 October 1998

5 Declaration of a variable that can contain an array
int g; g float averages; averages Face faces; face Declaring a variable does not “allocate” or create the array of elements; it only declares a variable that can contain a reference to an array of elements. An array is much like a class in that an array variable contains a reference to an array. null null null CS100A, Lecture 11, 6 October 1998

6 Allocating an array of elements
g = new int [5]; g Face f = new Face[206]; f What does execution of f[3]= new Face(…); do? Given allocated array f, we can reference f[0], f[1], … f[205]. But also use any expression for the subscript: f[i], f[2*i], etc. null null null null null null null CS100A, Lecture 11, 6 October 1998

7 Read in grades, print in reverse order
// Read in a list of integer grades, preceded by the // number of grades, and print in them in reverse order public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); int n= in.readInt(); // number of grades int[ ] g= new int [n]; // g[0..n-1] are the grades // Read in the grades int i= 0; // Inv: i grades have been read in and are in g[0..i-1] while (i != g.length) { g[i]= in.readInt( ); i= i+1; } // Print grades in reverse order int k= n; // Inv: grades in g[k..length-1] have been printed while (k != 0) { System.out.println(g[k]); k= k-1; CS100A, Lecture 11, 6 October 1998

8 Program scheme to print “histogram” of grades
// Read in a list of grades in the range , preceded // by the number of grades, and // print out how many times each grade appears public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); int n= in.readInt(); // number of grades int[ ] f= new int [101]; // f[i] will contain the no. // of times grade f appears // Initialize frequencies f[0..100] to 0. // Read in the grades and make up array f. // Print the grades and their frequencies (print only // the grades in that appeared at least once in // the input) } CS100A, Lecture 11, 6 October 1998

9 Program to print “histogram” of grades
// Read in a list of grades in the range , preceded // by the number of grades, and // print out how many times each grade appears public static void main (String arg[]) { TokenReader in = new TokenReader(System.in); int n= in.readInt(); // number of grades int[ ] f= new int [101]; // f[i] will contain the no. // of times grade f appears // Initialize frequencies f[k] to 0. int k= 0; // Inv: Each element of f[0..k-1] is 0 while (k != f.length) {f[k]= 0; k= k+1;} (continued on next slide) CS100A, Lecture 11, 6 October 1998

10 Program to print “histogram” of grades (continued)
// Read in the grades and make up array f int i= 0; // Inv: i grades have been read in and // each f[k], for 0<=k<=100, contains // the no. of times grade k was read in while (i != f.length) { int grade= in.readInt( ); f[ grade]= f[grade] + 1; i= i+1; } // Print the grades and their frequency (print only the // grades in that appeared at least once in the // input) // Inv: the grades 0..i-1 that occurred at least // once have been printed if (f[i] != 0) System.out.println(“grade: ” + i + “ frequency: ” + f[i]); i= i+1; CS100A, Lecture 11, 6 October 1998

11 Is a string a word a palindrome?
A palindrome is a word that reads the same backwards and forwards. The empty string A AA ABA NOON The following palindromes if blanks and punctuation are ignored able was I ere I saw elba a man a plan a canal panama Look at the web site for this lecture to see the longest palindrome that we know of --is it loooooooooooong! CS100A, Lecture 11, 6 October 1998

12 0 i j length-1 x rev. of x // Return the value of the statement
// “array b is a palindrome” static public bool isPalindrome(char[ ] b) { int i= 0; int j= b.length; // Invariant: b is a palindrome iff b[i..j-1] is. In other // words, b[j..length-1] is the reverse of b[0..i-1] while ( j-i >1) { j= j-1; if (b[i] != b[j]) return false; i= i+1; } // {b[i..j-1] has 0 or 1 elements, so it’s a palindrome} return true; i j length-1 x rev. of x CS100A, Lecture 11, 6 October 1998


Download ppt "Introduction to Arrays"

Similar presentations


Ads by Google