170 likes | 393 Views
Chapter 9 Nested Loops and Two-Dimensional Arrays. Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold. Chapter Preview. In this chapter we will: show how nested loops are useful introduce two-dimensional arrays
E N D
Chapter 9Nested Loops and Two-Dimensional Arrays Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E. Reingold
Chapter Preview In this chapter we will: • show how nested loops are useful • introduce two-dimensional arrays • describe the use of two-dimensional arrays to represent grids of information • show how computer graphics are generated using pixels
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 • Example: for (i = 0; i < 3; i++) { out.print(“i= “; + i + “: j = “); for (j = 0; j <= i; j++) { out.print(“ “ + j); } }
Nested Loop Contained in Other Statements 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(); }
Output ##### * #### ** ### *** ## **** # *****
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; double[][] energyTable = new double[ROWS][COLS]
Computing Row Totals double [] yearTotals = new double[ROWS]; for (y = 0; y < ROWS; y++) { // compute total for year y yearTotals[y] = 0.0; for (s =0; s < COLS; s++) yearTotals[y] = yearTotals[y] + energyTotal[y][s]; }
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++) energyTable[y][s] = in.readDouble();
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];
Computer Graphics • Computer graphics is the study of methods of representing and manipulating images • A two-dimensional array can be used to represent the image to be displayed • This array is called a frame buffer ; it has one entry for each pixel giving its color • The number of pixels in the frame buffer is the resolution the display device
Bresenham’s Line Drawing Algorithm • Draw (x0, y0) and compute p0 = 2y - x • Repeat for values of i from 1 to x – 1 : • Calculate xi+1 = xi + 1 • Calculate yi+1 = yi + 1, if pi > 0 yi , otherwise • Draw a pixel at (xi+1, yi+1) • Compute pi = pi + 2y - 2x(yi+1 - yi)
Two-Dimensional Arrays and length • A.length is number of rows in two-dimensional array A • A[i].length is number of columns in row i from two-dimensional array A • In Java rows can be of different lengths • Example: int[][] A = new int[5][]; for (int i = 0; i < 5; i++) { A[i] = new int[i + 1]; }