10
CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: < initialization to make invariant true> // general picture, or invariant: ... while ( B ) <body S, which makes progress toward termination but keeping the invariant true> // B is false, and from this and the invariant // we can determine that the result has been // calculated

CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

Embed Size (px)

Citation preview

Page 1: CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

CS100A, Fall 1997. Lecture 8

1

CS100A, Fall 1997

Lecture 8, Thursday, 25 September.

More on Iteration

We’ll develop some loops of the form:

< initialization to make invariant true>

// general picture, or invariant: ...

while ( B )

<body S, which makes progress toward

termination but keeping the invariant

true>

// B is false, and from this and the invariant

// we can determine that the result has been

// calculated

Page 2: CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

CS100A, Fall 1997. Lecture 8

2

Write a method with the following heading:

// Yield 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 yielded

‘a’ “All’s well that ends” 13

‘A’ “All’s well that ends” 0

‘i’ “All’s well that ends” 20

‘w’ “” 0

‘w’ “ “ 1

‘ ‘ “ “ 0

Page 3: CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

CS100A, Fall 1997. Lecture 8

3

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

General picture: c is not in s[0..j-1], i.e. c is not one of the first j characters of s, and

0 <= j <= s.length().

Initialization: j= 0;

When can the loop stop?

When j= s.length() or c = s[j].

So the loop condition is

j != s.length() && s.charAt(j) != c

Not Java notation

Page 4: CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

CS100A, Fall 1997. Lecture 8

4

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 s[0..j-1] and

// 0 <= j <= s.length().

while (j != s.length() && c != s.charAt(j))

j= j+1;

return j;

}

Page 5: CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

CS100A, Fall 1997. Lecture 8

5

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 s[0..j-1] and

// 0 <= j <= s.length().

while (j != s.length())

{if c = s.charAt(j)

return j;

j= j+1;

}

return j;

}

Page 6: CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

CS100A, Fall 1997. Lecture 8

6

Example of a loop: logarithmic spiral

containing n lines

1: length d2: length 2*d3: length 3*d…k: length k*d...

1

2

3

4

Each line turns turn degreesto the left of its predecessor

turn degrees

(hc,vc)blue

blue

red

green

Page 7: CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

CS100A, Fall 1997. Lecture 8

7

// The spiral consists of n line segments.

// Line segment 1 has starting point (hc, vc).

// Line segment k, for 1<=k<=n, has length k*d.

// 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 k*d

We demonstrate execution of this program on the Macintosh, using also n = 10,000 and different values of turn (95, 90, 180, 150),

Page 8: CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

CS100A, Fall 1997. Lecture 8

8

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 (k<=n)

{//Draw line k

if (k%3==0) g.setColor(Color.red);

if (k%3==1) g.setColor(Color.blue);

if (k%3==2) g.setColor(Color.green);

int theta= k*turn %360;

double L= k*d; // Length of line k

// Compute end (h_next,v_next) of line k

int h_next= (int) Math.round (

h+L*Math.cos(theta*Math.PI/180));

int v_next= (int) Math.round (

v+L*Math.sin(theta*Math.PI/180));

g.drawLine(h,v,h_next, v_next);

h= h_next; v= v_next; k= k+1;

}

}

Page 9: CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

CS100A, Fall 1997. Lecture 8

9

Problem: Given n>0,

set s to the largest power of 2 that is at most n.

n s

1 1 2^0 = 1

2 2 2^1 = 2

3 2

4 4 2^2 = 2*2 = 4

5 4

6 4

7 4

8 8 2^3 = 2*2*2 = 8

9 8

50 32 2^5 = 2*2*2*2*2 = 32

Page 10: CS100A, Fall 1997. Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general

CS100A, Fall 1997. Lecture 8

10

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

s is a power of 2 and s <= n

Initialization: s= 1;

Stop when: 2*s > n

Continue as long as: 2*s <= n

Make progress toward termination and keep

general picture true: s= 2*s;

// Known: n>0. Set s to the largest power of

// 2 that is at most n

s= 1;

//invariant: 2 is a power of 2 and s <= n

while (2*s <= n)

s= 2*s;