Chapter 9 Nested Loops and TwoDimensional Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 9 Nested Loops and TwoDimensional Arrays

Description:

Bresenham's Line Drawing Algorithm. Draw (x0, y0) and compute p0 = 2 ... Draw a pixel at (xi 1, yi 1) Compute pi = pi 2 y - 2 x(yi 1 - yi) Two-Dimensional Arrays ... – PowerPoint PPT presentation

Number of Views:56
Avg rating:3.0/5.0
Slides: 18
Provided by: brucem151
Category:

less

Transcript and Presenter's Notes

Title: Chapter 9 Nested Loops and TwoDimensional Arrays


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 lt m I)
  • before inner loop
  • for (j 0 j lt 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 lt 3 i)
  • out.print(i i j )
  • for (j 0 j lt i j)
  • out.print( j)

5
(No Transcript)
6
Nested Loop Contained in Other Statements
  • for (int i 1 i lt 10 i)
  • if (i 2 0) // i even
  • for (int j 1 j lt i/2 j)
  • out.print()
  • else // i odd
  • for (int k 1 k lt 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 doubleROWSCOLS

9
(No Transcript)
10
Computing Row Totals
  • double yearTotals new doubleROWS
  • for (y 0 y lt ROWS y)
  • // compute total for year y
  • yearTotalsy 0.0
  • for (s 0 s lt COLS s)
  • yearTotalsy
  • yearTotalsy energyTotalys

11
Populating energyTable
  • int y, s
  • // reads 30 numbers needed to fill
  • // energyTable one row at a time
  • for (y 0 y lt ROWS y)
  • for (s 0 s lt COLS s)
  • energyTableys 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
(No Transcript)
14
Arrays of Arrays
  • When we write
  • energyTable new doubleROWSCOLS
  • This is shorthand for
  • energyTable new doubleROWS
  • for (int i 0 i lt ROWS i)
  • energyTableI new doubleCOLS

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
Bresenhams Line Drawing Algorithm
  • Draw (x0, y0) and compute p0 2?y - ?x
  • Repeat for values of i from 1 to ?x 1
  • Calculate xi1 xi 1
  • Calculate yi1 yi 1, if pi gt 0
  • yi ,
    otherwise
  • Draw a pixel at (xi1, yi1)
  • Compute pi pi 2?y - 2?x(yi1 - yi)

17
Two-Dimensional Arrays and length
  • A.length is number of rows in two-dimensional
    array A
  • Ai.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 int5
  • for (int i 0 i lt 5 i)
  • Ai new inti 1
Write a Comment
User Comments (0)
About PowerShow.com