CS100A,%20Lect.%209,%2029%20Sept.%201997 - PowerPoint PPT Presentation

About This Presentation
Title:

CS100A,%20Lect.%209,%2029%20Sept.%201997

Description:

Line colors alternate between blue, green, red ... public void paint(Graphics g) { int h= hc; int v= vc; int k= 1; ... (Color.red); if (k%3==1) g.setColor(Color. ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 11
Provided by: david148
Category:

less

Transcript and Presenter's Notes

Title: CS100A,%20Lect.%209,%2029%20Sept.%201997


1
CS100A, Lect. 9, 29 Sept. 1997
  • More on Iteration
  • Well develop some loops of the form
  • lt initialization to make invariant truegt
  • // general picture, or invariant P ...
  • while ( B )
  • ltbody S, which makes progress toward
  • termination but keeping P truegt
  • // B is false, and from this and invariant P
  • // we can see that the result has been
  • // achieved

2
  • Problem Given ngt0,
  • set s to the largest power of 2 that is at most
    n.
  • n s
  • 1 1 20 1
  • 2 2 21 2
  • 3 2
  • 4 4 22 22 4
  • 5 4
  • 6 4
  • 7 4
  • 8 8 23 222 8
  • 9 8
  • 50 32 25 22222 32
  • 63 32
  • 64 64 26 222222 64
  • 65 64

3
  • Strategy Start s at 1 and continue to multiply
    it by 2 until the next multiplication by 2 would
    make it too big.
  • General picture (invariant)
  • s is a power of 2 and s lt n
  • Initialization s 1
  • Stop when 2s gt n
  • Continue as long as 2s lt n
  • Make progress toward termination and keep
  • general picture true s 2s
  • // Known ngt0. Set s to the largest power of
  • // 2 that is at most n
  • s 1
  • //invariant 2 is a power of 2 and s lt n
  • while (2s lt n)
  • s 2s

4
  • Example of a loop logarithmic spiral
  • containing n lines

2
green
turn degrees
red
3
(hc,vc)
blue
1
4
blue
1 length d 2 length 2d 3 length 3d k
length kd ...
Each line turns turn degrees to the left of its
predecessor
5
  • // The spiral consists of n line segments.
  • // Line segment 1 has starting point (hc, vc).
  • // Line segment k, for 1ltkltn, has length kd.
  • // Each line segment makes an angle of turn
    degrees
  • // with the previous line segment.
  • // Line colors alternate between blue, green, red
  • final static int hc 300 // Center of spiral is
    (hc,vc)
  • final static int vc 250
  • final static int n 200 // Number of sides
    to draw
  • final static int turn 121 // The turn factor
  • final static double d .2 // Length of leg k is
    kd
  • We demonstrate execution of this program on the
    Macintosh, using also n 10,000 and different
    values of turn (85, 89, 90, 85, 135, 180, 150),

6
  • public void paint(Graphics g)
  • int h hc int v vc
  • int k 1
  • //Invariant lines 1..k-1 have been drawn, and
    line k
  • // is to be drawn with start
    point (h,v)
  • while (kltn)
  • //Draw line k
  • if (k30) g.setColor(Color.red)
  • if (k31) g.setColor(Color.blue)
  • if (k32) g.setColor(Color.green)
  • int theta kturn 360
  • double L kd // Length of line k
  • // Compute end (h_next,v_next) of line k
  • int h_next (int) Math.round (
  • hLMath.cos(thetaMath.PI/180))
  • int v_next (int) Math.round (
  • vLMath.sin(thetaMath.PI/180))
  • g.drawLine(h,v,h_next, v_next)

7
  • Write a method with the following heading
  • // Return the position of c in s (or s.length()
  • // if c is not in s)
  • public static int find(char c, String s)
  • Examples
  • c s result
  • a Alls well that ends 13
  • A Alls well that ends 0
  • i Alls well that ends 20
  • w 0
  • w 1
  • 0

8
  • Strategy look at the characters of s one by one,
    starting from the beginning. Return as soon as c
    is found. Use a variable j to tell how much of s
    has been scanned (looked at).
  • General picture c is not in s0..j-1, i.e. c is
    not one of the first j characters of s, and
  • 0 lt j lt s.length().
  • Initialization j 0
  • When can the loop stop?
  • When j s.length() or c sj.
  • So the loop condition is
  • j ! s.length() s.charAt(j) ! c

Not Java notation
9
  • What should be done in the body to get closer to
    termination?
  • Add one to j.
  • What should be done in the body to keep the
    general picture true? Nothing.
  • // Yield the position of c in s (yield s.length()
  • // if c is not in s)
  • public static int find(char c, String s)
  • int j 0
  • // Invariant c is not in s0..j-1 and
  • // 0 lt j lt s.length().
  • while (j ! s.length( ) c ! s.charAt(j))
  • j j1
  • return j

10
  • Another version
  • // Yield the position of c in s (yield s.length()
  • // if c is not in s)
  • public static int find(char c, String s)
  • int j 0
  • // Invariant c is not in s0..j-1 and
  • // 0 lt j lt s.length().
  • while (j ! s.length( ))
  • if c s.charAt(j)
  • return j
  • j j1
  • return j
Write a Comment
User Comments (0)
About PowerShow.com