1 / 17

Chapter 9 Nested Loops and Two-Dimensional Arrays

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

jela
Download Presentation

Chapter 9 Nested Loops and Two-Dimensional Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

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

  6. Output ##### * #### ** ### *** ## **** # *****

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

  8. 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]; }

  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++) energyTable[y][s] = in.readDouble();

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

  11. 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];

  12. 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

  13. Bresenham’s Line Drawing Algorithm • Draw (x0, y0) and compute p0 = 2y - 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 + 2y - 2x(yi+1 - yi)

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

More Related