Nested Loop Review and TwoDimensional Arrays Learning Objectives
Nested Loop Review and Two-Dimensional Arrays
Learning Objectives • Review nested loops • Introduce two-dimensional arrays • Describe the use of two-dimensional arrays to represent grids of information
Nested for Loops • Nested loops frequently used to process twodimensional arrays • Often body of inner loop is where the main computation is done • Example: for (i = 0; i < m; I++) { before inner loop for (j = 0; j < n; j++) body of inner loop after inner loop };
Dependent for Loops • Sometimes the extent of the inner nested loop will depend on the index value of the outer loop • Dry Run the following: for (i = 0; i < 3; i++) { System. out. print(“i= “; + i + “: j = “); for (j = 0; j <= i; j++) { System. out. print(“ “ + j); } }
Nested Loop Contained in Other Statements //Dry run #2 for (int i = 1; i <= 10; { if (i % 2 == 0) // for (int j = 1; j <= out. print(“*”); else // for (int k = 1; k <= out. print(“#”); out. println(); } i++) i even i/2; j++) i odd 5 – i/2; k++)
Two-Dimensional Arrays • Declaration similar to one dimensional arrays • Need to specify both the number of rows and columns during allocation • Example: final int COLS = 6, ROWS = 5; //Constants double[][]energy. Table = new double[ROWS][COLS];
Computing Row Totals double [] year. Totals = new double[ROWS]; for (int year = 0; year < ROWS; year++) { // compute total for the row year. Totals[year] = 0. 0; for (int column =0; column < COLS; column++) year. Totals[year] = year. Totals[year] + energy. Total[year][column]; }
Populating energy. Table int y, s; // reads 30 numbers needed to fill // energy. Table one row at a time for (y = 0; y < ROWS; y++) for (s = 0; s < COLS; s++) { System. out. println(“Enter the next value”); energy. Table[y][s] = input. next. Double(); }
Initializing Two-Dimensional Arrays double[][] { {18. 9, {19. 1, {18. 8, {18. 9, {19. 6, }; energy. Table = 19. 4, 19. 3, 19. 6, 20. 3, 20. 8, 34. 2, 33. 6, 32. 9, 33. 5, 33. 8, 3. 9, 3. 0, 3. 1, 2. 8, 3. 1, 5. 7, 6. 2, 6. 6, 6. 7, 6. 5, 0. 3}, 0. 2}, 0. 2}
Arrays of Arrays When we write energy. Table = new double[ROWS][COLS]; This is shorthand for energy. Table = new double[ROWS][]; for (int i = 0; i < ROWS; i++) energy. Table[i] = new double[COLS];
2 -D Arrays: Dimensions • In Java, a 2 -D array is a 1 -D array of 1 -D arrays, its rows. Each row is stored in a separate block of consecutive memory locations. • If m is a 2 -D array, then m[k] is a 1 -D array, the k-th row. • m. length is the number of rows. • m[k]. length is the length of the k-th row. 12 -13
public static void main(String [] args) { final char BLANK = '? '; // location empty final char X = 'x'; final char OH = 'o'; final int size = 3; char board[][] = new char[size]; for (int i=0; i < size; i++) for (int j=0; j < size; j++) board[i][j] = BLANK; for(int i=0; i< size; i++) board[i][i] = X; for (int j = 0; j< size; j++) board[j][size-j-1] = OH; for (int i=0; i < size; i++) { for (int j=0; j < size; j++) System. out. print(board[i][j]+" "); System. out. println(); } } Dry Run
More Two D • Internally, Java stores 2 dimensional arrays as an array • • of arrays: int [][] nums = new int[5][4]; The above is really equivalent to a 3 -step process: // create the single reference nums (yellow square) int [][] nums; // create the array of references (blue squares) nums = new int[5][]; // this create the second level of arrays (red squares) Ø for (int i=0; i < 5 ; i++) • nums[i] = new int[4]; // create arrays of integers
Even More 2 -D • • • Note: when you initially declare a 2 D array: you must always specify the first dimension nums = new int[][]; // ILLEGAL - NEEDS 1 ST DIMENSION you do not need to specify the second dimension nums = new int[5][]; // OK nums = new int[5][4]; // OK Elements of the Array: if nums is a 2 D array as shown above, nums[i][j] represents a single integer in that array nums[i] represents a 1 D array (a single row in the 2 D array)
Adding Another Dimension • You can create arrays of higher dimension than 2. For • • example, if we were measuring the temperature in a rectangular volume. int temperature[][][] = new int[10][20][30]; This creates an array of 10 x 20 x 30=6000 integers. temperature is an array of arrays Sub. Arrays: Ø Ø Ø temperature is a 3 D array of size 10 x 20 x 30. There is one of these. temperature[i] is a 2 D array of size 20 x 30. There are 10 of these. temperature[i][j] is a 1 D array of size 30. There are 200 of these. • All but the last dimension must be initially specified: • int temperature[][][] = new int[10][]; // OK • int temperature[][][] = new int[10][][]; // NOT OK
2 D Array Magic Program • A Magic Square is a two-dimensional array of positive integers such that the sum of each row, column and diagonal is the same. Write a program that takes 9 integers as inputs. The program should determine whether or not the square is a magic square and display the results. • Example: 2 7 6 9 5 1 4 3 8 • Push: Modify your program to check a 4 x 4 Square. • Push: 5 x 5 • Push: N x N 12 -18
• Tic-Tac-Toe Ø Ø Level 1: User vs. Computer Level 2: Add computer checking for winner Level 3: Add logic to computer choices Level 4: 3 -D • Battleship Ø Ø Level 1: User vs. Computer Level 2: Add computer checking for winner Level 3: Add logic to computer choices Level 4: 3 -D • Checkers Ø Ø Level 1: User vs. Computer Level 2: Add computer checking for winner Level 3: Add logic to computer choices Level 4: 3 -D • Chess Ø Ø Level 1: User vs. Computer Level 2: Add computer checking for winner Level 3: Add logic to computer choices Level 4: 3 -D Multi. Dimensional Array Project
Getting a Single char From User • Scanner input = new Scanner (System. in); • char c = input. next(). char. At(0);
- Slides: 20