Chapter 9 - Nested loops and two-dimensional arrays. 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.
Trang 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
Trang 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
Trang 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
};
Trang 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);
}
}
Trang 5Trang 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();
}
Trang 7
Output
#####
*
####
**
###
***
##
****
#
*****
Trang 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]
Trang 9Trang 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]; }
Trang 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();
Trang 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} };
Trang 13Trang 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];
Trang 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
Trang 16
Bresenham’s Line Drawing
Algorithm
1 Draw (x0, y0) and compute p0 = 2 y - x
2 Repeat for values of i from 1 to x – 1 :
a) Calculate xi+1 = xi + 1 b) Calculate yi+1 = yi + 1, if pi > 0
yi , otherwise c) Draw a pixel at (xi+1, yi+1)
d) Compute pi = pi + 2 y - 2 x(yi+1 - yi)
Trang 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];
}