Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 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

2 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

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 Example: for (i = 0; i < 3; i++) { out.print(“i= “; + i + “: j = “); for (j = 0; j <= i; j++) { out.print(“ “ + j); }

5

6 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(); }

7 Output ##### * #### ** ### *** ## **** # *****

8 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]

9

10 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]; }

11 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();

12 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} };

13

14 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];

15 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

16 Bresenham’s Line Drawing Algorithm 1.Draw (x 0, y 0 ) and compute p 0 = 2  y -  x 2.Repeat for values of i from 1 to  x – 1 : a)Calculate x i+1 = x i + 1 b)Calculate y i+1 = y i + 1, if p i > 0 y i, otherwise c)Draw a pixel at (x i+1, y i+1 ) d)Compute p i = p i + 2  y - 2  x(y i+1 - y i )

17 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]; }


Download ppt "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,"

Similar presentations


Ads by Google