27 Nov 06 present: ic, nj, mm, kt, sw, ip I. New statements learnt this week: a) define an array of certain type: one -dimensional: [] = new [] two - dimensional: [][] = new [][] b) array element i: A[i], A[5], A[6] etc. c) Examples of an array: What is an array? What is an one-dimonsional array? i) an array of integers: index 0 1 2 3 4 5 A 23 2 1 10 6 4 ii) an array of char: (chars should be quoted by `', e.g. `a') index 0 1 2 3 4 5 A a b c d e f II. Problems: 1) define an integer array A up to 100 indices: int [] A = new int [100]; 2) points to the 3rd and 5th elements in example c)ii): A[2], A[4] 3) display element i: System.out.println(A[i]); 4) display all the elements in A[0..8]: for (int i=0; i<9; i++) { System.out.println(A[i]); } 5) display all the elements in reverse order: for (int i=8; i>=0; i--) { System.out.println(A[i]); } 6) compute and display the total of the elements A[3--8]: int total = 0; for (int i=3; i<9; i++) { total = total+A[i]; System.out.println(A[i]); } System.out.println(total); 7) input 10 integers from the keyboard, and store them in array A: import java.util.Scanner; Scanner keyboard = new Scanner(System.in); for (int i=0; i<10; i++) { A[i] = keyboard.nextInt(); } for (int i=0; i<10; i++) { System.out.println(A[i]); } 8) store 10 random integers ranging [2,5] in an array A, and display them. import java.util.Random; Random r = new Random(); for (int i=0; i<10; i++) { A[i] = 2+r.nextInt(3); } for (int i=0; i<10; i++) { System.out.println(A[i]); } 9) check if a value 0 is in the array A, display "zero" if yes, and "no 0" if no. boolean z = false; for (int i=0; i<10; i++) { if (A[i]==0) { z=true; break; } } if (z) { System.out.println("Zero!"); } else { System.out.println("No zero."); } 10) Compute A+B, A-B, A*B and display the results. For example, let array A={1,2,3}, B={2,1,3}. A+B={1+2,2+1,3+3}={3,3,6}; A-B={1-2,2-1,3-3}={-1,1,0}; // note the condition of matrix multiplication. A*B' ={1 2 3}*{2 1 3} ={1*2+2*1+3*3} = {13}; A'*B = {1 2 3}{2 1 3}= {1*2, 1*2, 1*3 = {2,1,3 2*2, 2*1, 2*3 4,2,6 3*2, 3*1, 3*3} 6,3,9} --------- class multiplication1 { public static void main(String [] args) { int [][] C = new int [3][3]; int A[]={1,2,3}; int B[]={2,1,3}; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { C[i][j] = A[i]*B[j]; } } for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { System.out.print(C[i][j]+" "); } System.out.println(); } } } ------------ a) C=A+B, and D=A-B: --------- int [] A = new int [3]; int [] B = new int [3]; int [] C = new int [3]; int [] D = new int [3]; for (int i=0; i<3; i++) { C[i] = A[i]+B[i]; D[i] = A[i]-B[i]; } b) two-dimensional array: import java.util.Random; int [][] C = new int [3][3]; int [] A = new int [3]; int [] B = new int [3]; Random r = new Random(); for (int i=0; i<3; i++) { A[i] = 1+r.nextInt(2); B[i] = 1+r.nextInt(2); } for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { C[i][j] = A[i]*B[j]; } } for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { System.out.println(C[i][j]); } } ---------- III. Implementation: Problem 1) ---- class test1 { public static void main(String [] args) { int [] A = new int [100]; for (int i=1; i<10; i++) { System.out.println(A[i]); } } } ---- Problem 2) ----- import java.util.Random; class test2 { public static void main(String [] args) { Random r = new Random(); int [] A = new int [100]; for (int i=0; i<10; i++) { A[i]=r.nextInt(10); } for (int i=0; i<10; i++) { System.out.println(A[i]); } System.out.println("2:"+A[2]+" "+"4:"+ A[4]); } } ----- Problem 3): ---- import java.util.Random; class test3 { public static void main(String [] args) { Random r = new Random(); int [] A = new int [100]; for (int i=0; i<10; i++) { A[i]=r.nextInt(10); } for (int i=0; i<10; i++) { System.out.println(i+"\t"+A[i]); } int i = r.nextInt(9); System.out.println(i+": "+A[i]); } } ---- Problem 4) ---- import java.util.Random; class test4 { public static void main(String [] args) { Random r = new Random(); int [] A = new int [100]; for (int i=0; i<10; i++) { A[i]=r.nextInt(10); } for (int i=0; i<10; i++) { System.out.println(i+"\t"+A[i]); } } } ---- Problem 5) ---- import java.util.Random; class test5 { public static void main(String [] args) { Random r = new Random(); int [] A = new int [100]; for (int i=0; i<10; i++) { A[i]=r.nextInt(10); } for (int i=0; i<10; i++) { System.out.println(i+"\t"+A[i]); } System.out.println(); for (int i=9; i>=0; i--) { System.out.println(i+"\t"+A[i]); } } } ---- Problem 6) ---- import java.util.Random; class test6 { public static void main(String [] args) { Random r = new Random(); int [] A = new int [100]; for (int i=0; i<10; i++) { A[i]=r.nextInt(10); } for (int i=0; i<10; i++) { System.out.println(i+"\t"+A[i]); } System.out.println(); int total = 0; for (int i=3; i<9; i++) { total = total + A[i]; } System.out.println("total A[3..8]=\t"+total); } } ---- Problem 7) ---- import java.util.Scanner; class test7 { public static void main(String [] args) { int [] A = new int [100]; Scanner keyboard = new Scanner(System.in); for (int i=0; i<10; i++) { A[i] = keyboard.nextInt(); } for (int i=0; i<10; i++) { System.out.println(A[i]); } System.out.println(); for (int i=0; i<10; i++) { System.out.println(i+"\t"+A[i]); } System.out.println(); } } ---- Problem 8) ---- import java.util.Random; class test8 { public static void main(String [] args) { int [] A = new int [100]; Random r = new Random(); for (int i=0; i<10; i++) { A[i] = 2+r.nextInt(3); } for (int i=0; i<10; i++) { System.out.println(A[i]); } System.out.println(); for (int i=0; i<10; i++) { System.out.println(i+"\t"+A[i]); } System.out.println(); } } ---- Problem 9) ---- import java.util.Random; class test9 { public static void main(String [] args) { int [] A = new int [100]; Random r = new Random(); for (int i=0; i<10; i++) { A[i] = r.nextInt(10); } for (int i=0; i<10; i++) { System.out.println(i+"\t"+A[i]); } System.out.println(); boolean z = false; for (int i=0; i<10; i++) { if (A[i]==0) { z=true; break; } } if (z) { System.out.println("Zero!"); } else { System.out.println("No zero."); } } } ---- Problem 10)a): ---- import java.util.Random; class test10a { public static void main(String [] args) { int [] A = new int [100]; int [] B = new int [100]; Random r = new Random(); for (int i=0; i<10; i++) { A[i] = 1+r.nextInt(9); B[i] = 1+r.nextInt(9); } for (int i=0; i<10; i++) { System.out.println(i+":\t"+A[i]+"\t"+B[i]); } System.out.println(); for (int i=0; i<10; i++) { System.out.println(i+"\t"+A[i]+"+"+B[i]+"=\t"+(A[i]+B[i])); } System.out.println(); } } ---- Problem 10)b): ---- import java.util.Random; class multiplication { public static void main(String [] args) { Random r = new Random(); int [][] C = new int [3][3]; int [] A = new int [3]; int [] B = new int [3]; for (int i=0; i<3; i++) { A[i] = 1+r.nextInt(2); B[i] = 1+r.nextInt(2); } for (int i=0; i<3; i++) { System.out.print(A[i]+" "); } System.out.println(); for (int i=0; i<3; i++) { System.out.print(B[i]+" "); } System.out.println(); for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { C[i][j] = A[i]*B[j]; } } for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { System.out.print(C[i][j]+" "); } System.out.println(); } } } -----------------