Download presentation
Presentation is loading. Please wait.
Published byἈχιλλεύς Κουρμούλης Modified over 6 years ago
1
Nested Loop Review and Two-Dimensional Arrays
2
Learning Objectives Review nested loops
Introduce two-dimensional arrays Describe the use of two-dimensional arrays to represent grids of information
3
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 };
4
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); }
5
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(); }
6
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];
8
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]; }
9
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(); }
10
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} };
12
Arrays of Arrays When we write This is shorthand for
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];
13
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. Nothing unusual: an array of rows contains objects; each object happens to be an array.
14
public static void main(String [] args) { final char BLANK = '
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; System.out.print(board[i][j]+" "); System.out.println(); } Dry Run
15
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
16
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)
17
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
18
2D Array Magic Square 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. You decide how the order of input matches the square. 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 Push: Write a program that will have the computer randomly generate a magic square 2 7 6 9 5 1 4 3 8
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.