Transcript ppt

Sections Oct 20-21
Two-dimensional arrays
So far, arrays have been 1D arrays. They represent a list
of values. 2D arrays allow us to represent values in 2
dimensions (rows and columns).
5
3
8
-1
7
-5
• Technically, Java does not support
multidimensional arrays, e.g., 2D arrays.
• In practice, multidimensional arrays are
supported because a 1D array can have an
array as an element.
Declare and allocate two-dimensional array:
int [ ] [ ] b;
b = new int [3] [2];
Note: b.length is 3 and b[0].length is 2.
Assigning to array elements could yield:
b[0]
b[0][0]
b[0][1]
5
3
b[1]
b[1][0]
b[1][1]
8
-1
b[2]
b[2][0]
b[2][1]
7
-5
int [ ] [ ] b = { {5, 3}, {8, -1}, {7, -5} };
The for loop:
for ( initialization; condition; increment )
S;
1. Execute the initialization statement.
2. Evaluate the condition.
3. If false, continue w/first statement after the loop.
If true, execute the body.
4. After the body, execute the increment statement.
for (i= 0; i != n; i= i+1)
S;
is equivalent to:
i= 0;
while (i != n) {
S; i= i+1;
}
// Assign 0 to all elements of b
public static void zeroOut(int b[][]) {
for (int r= 0; r != b.length; r= r+1)
for (int c= 0; c != b[r].length; c= c+1)
b[r][c]= 0;
}
Pascal’s Triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
int [] [] p;
p = new int[7][];
p[0] = new int[1];
p[1] = new int[2];
p[2] = new int[3];
// p.length is 7
// p[0].length is 1
// p[1].length is 2
// p]2].length is 3
Elements of an array can be
arrays of different lengths!
// Yield Pascal's triangle with size rows
public static int[][] calculatePascal(int size) {
int[][]p= new int[size][]; //the triangle
// Invariant: rows 0..r-1 have been
//
allocated and calculated
for (int r= 0; r != size; r= r+1) {
// Allocate row i of triangle --its r+1 values
p[r]= new int[r + 1];
// Calculate row r of Pascal's triangle
p[r][0]= 1;
for (int c= 1; c < r; c= c+1)
p[r][c]= p[r-1][c-1] + p[r-1][c];
p[r][r]= 1;
}
return p;
}
// Print Pascal's triangle p, assuming that its
//values are less than 1000
static public void printPascal(int p[][]) {
int size= p.length;
for (int r= 0; r != size; r= r+1) {
// Add ((size-r)/2)4 + ((r+1) % 2)*2
// blanks to s
String s= "";
int num;
if (size%2 == 1) num= (size-r)/2;
else
num= (size-r-1)/2;
for (int c= 0; c< num; c= c+1)
s= s + " ";
if (r%2 == 0) s= s + " ";
// Add the numbers in row r to s
for (int c= 0; c <= r; c= c+1)
s= s + printbrc(p, r,c,1000);
System.out.println(s);
}
}
// Return element b[r][c] of array b as a String,
// with a blank before it.
// Use as many columns as is required to print x.
// So, if x = 325, use 4 characters in total (one
// for the initial blank).
// Precondition, x < 1000
static public String printbrc(
int[][] b, int r, int c, int x)
Example of dealing with a
two-dimensional array: Magic Squares
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
each row sums to 65
each col sums to 65
each diagonal sums
to 65
In Albert Duerer’s
engraving
“Melancolia”, done
in 1514!
17
23
4
10
11
24
5
6
12
18
1
7
13
19
25
8
14
20
21
2
15
16
22
3
9
each row sums to 65
each col sums to 65
each diagonal sums
to 65
Encyclopedia Britannica: The smallest possible
square of odd order has, of course, side-length 3!
To construct n x n magic square n x n ( n odd):
0. Put 1 in b[0][n%2]
1. If i is in b[r][c], then i+1 goes in b[r’][c’],
where r’,c’ determined as follows:
(a) try r’= r-1, if -1, then use r’=n-1.
(b) try c’= c+1, if n, then use c’= 0.
(c) if b[r’][c’] is already filled, use
r’=r+1, c’= c
// Store a magic square in b
static public void magicSquare(int b[][]) {
int r= 0; int c= 0; int i; int size= b.length;
zeroOut(b);
// Make up the magic square
r= 0; c= size/2; i= 1;
// invariant: Array elements have already been filled
//
with 1, ..., i-1, and b[r][c] is supposed to get i.
while (b[r][c] == 0) {
b[r][c]= i; i= i+1;
if (b[mod(r-1, size)][mod(c+1, size)]
== 0) {
r= mod(r-1, size);
c= mod(c+1, size);
}
else r= mod(r+1,size);
}
}