Download presentation source

Download Report

Transcript Download presentation source

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
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
‘a’ “All’s well that ends”
‘A’ “All’s well that ends”
‘i’ “All’s well that ends”
‘w’ “”
‘w’ “ “
‘‘ ““
CS100A, Fall 1997.
Lecture 8
result yielded
13
0
20
0
1
0
2
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;
Not Java
notation
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
CS100A, Fall 1997.
Lecture 8
3
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;
}
CS100A, Fall 1997.
Lecture 8
4
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;
}
CS100A, Fall 1997.
Lecture 8
5
Example of a loop: logarithmic spiral
containing n lines
2
turn degrees
3
(hc,vc)
1
4
1: length d
2: length 2*d
3: length 3*d
…
k: length k*d
...
Each line turns turn degrees
to the left of its predecessor
CS100A, Fall 1997.
Lecture 8
6
// 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;
final static int vc= 250;
final static int n= 200;
final static int turn= 121;
final static double d= .2;
// Center of spiral is (hc,vc)
// Number of sides to draw
// The turn factor
// 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),
CS100A, Fall 1997.
Lecture 8
7
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;
}
}
CS100A, Fall 1997.
Lecture 8
8
Problem: Given n>0,
set s to the largest power of 2 that is at most n.
n
1
2
3
4
5
6
7
8
9
…
50
s
1
2
2
4
4
4
4
8
8
32
2^0 = 1
2^1 = 2
2^2 = 2*2 = 4
2^3 = 2*2*2 = 8
2^5 = 2*2*2*2*2 = 32
CS100A, Fall 1997.
Lecture 8
9
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;
CS100A, Fall 1997.
Lecture 8
10