200 likes | 210 Views
Learn about nested loops, two-dimensional arrays, and their applications. Explore examples, declarations, and computations with hands-on programming experience.
E N D
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 two-dimensional 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; i++) { if (i % 2 == 0) // i even for (int j = 1; j <= i/2; j++) out.print(“*”); else // i odd for (int k = 1; k <= 5 – i/2; k++) out.print(“#”); out.println(); }
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[][]energyTable = new double[ROWS][COLS];
Computing Row Totals double [] yearTotals = new double[ROWS]; for (int year = 0; year < ROWS; year++) { // compute total for the row year yearTotals[year] = 0.0; for (int column =0; column < COLS; column++) yearTotals[year] = yearTotals[year] + energyTotal[year][column]; }
Populating energyTable int y, s; // reads 30 numbers needed to fill // energyTable one row at a time for (y = 0; y < ROWS; y++) for (s = 0; s < COLS; s++) { System.out.println(“Enter the next value”); energyTable[y][s] = input.nextDouble(); }
Initializing Two-Dimensional Arrays double[][] energyTable = { {18.9, 19.4, 34.2, 3.9, 5.7, 0.3}, {19.1, 19.3, 33.6, 3.0, 6.2, 0.2}, {18.8, 19.6, 32.9, 3.1, 6.6, 0.2}, {18.9, 20.3, 33.5, 2.8, 6.7, 0.2}, {19.6, 20.8, 33.8, 3.1, 6.5, 0.2} };
Arrays of Arrays When we write energyTable = new double[ROWS][COLS]; This is shorthand for energyTable = new double[ROWS][]; for (int i = 0; i < ROWS; i++) energyTable[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.
Dry Run 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][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(); } }
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 (inti=0; i < 5 ; i++) • nums[i] = new int[4]; // create arrays of integers
Even More 2-D • Note: when you initially declare a 2D array: • you must always specify the first dimension • nums = new int[][]; // ILLEGAL - NEEDS 1ST 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 2D array as shown above, • nums[i][j] represents a single integer in that array • nums[i] represents a 1D array (a single row in the 2D 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 10x20x30=6000 integers. • temperature is an array of array of arrays • SubArrays: • temperature is a 3D array of size 10x20x30. There is one of these. • temperature[i] is a 2D array of size 20x30. There are 10 of these. • temperature[i][j] is a 1D array of size 30. There are 200 of these. • All but the last dimension must be initially specified: • int temperature[][][] = new int[10][10][]; // OK • int temperature[][][] = new int[10][][]; // NOT OK
2D 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: • Push: Modify your program to check a 4x4 Square. • Push: 5x5 • Push: N x N
Multi-Dimensional Array Project • 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
Getting a Single char From User • Scanner input = new Scanner (System.in); • char c = input.next().charAt(0);