Download presentation source

Download Report

Transcript Download presentation source

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
1
Data processing problem
Input: zero or more grades in the range 0..100, preceded
by the number of grades
Sample input: 5 90 85 93 40 89
Task: Read grades and print information about them:
• Print grades in print grades in the reverse of the order
given. e.g 89 40 93 85 90
• Print grades in increasing order. e.g. 40 85 89 90 93
• 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
2
The ARRAY helps solve the problem
g
0
1
90
85
2
3
93 40
4
89
g[0] is 90
g[1] is 85
g.length is the number of array
g[2] is 93
elements in array g
g[3] is 40
g.length = 5
g[4] is 89
in “g[4]”,
4 is the “subscript” or “index”
h
0
1
90
85
2
3
93 40
h.length
4
…
204 205
89
…
60
32
is the number of array
elements in array h
h.length = 206
CS100A, Lecture 11, 6
October 1998
3
Conventions
h
i
j
k
g
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
CS100A, Lecture 11, 6
October 1998
4
Declaration of a variable that can contain an array
int g;
float averages;
g
null
averages
null
face
null
Face faces;
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.
CS100A, Lecture 11, 6
October 1998
5
Allocating an array of elements
g = new int [5];
g
0
1
2
Face f = new Face[206];
0
1
2
3
3
4
f
4
null null null null null
…
204 205
...
null null
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.
CS100A, Lecture 11, 6
October 1998
6
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();
int[ ] g= new int [n];
// number of grades
// 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
7
Program scheme to print “histogram” of grades
// Read in a list of grades in the range 0..100, 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 0..100 that appeared at least once in
// the input)
}
CS100A, Lecture 11, 6
October 1998
8
Program to print “histogram” of grades
// Read in a list of grades in the range 0..100, 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
9
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 0..100 that appeared at least once in the
// input)
int i= 0;
// Inv: the grades 0..i-1 that occurred at least
//
once have been printed
while (i != f.length) {
if (f[i] != 0)
System.out.println(“grade: ” + i +
“ frequency: ” + f[i]);
i= i+1;
}
}
CS100A, Lecture 11, 6
October 1998
10
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
11
// 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]
0
i
j length-1
rev. of x
x
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;
}
CS100A, Lecture 11, 6
October 1998
12