insight through computing 7. the while-loop for-loop problems introduce while-loops
TRANSCRIPT
![Page 1: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/1.jpg)
Insight Through Computing
7. The While-Loop
For-Loop ProblemsIntroduce While-Loops
![Page 2: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/2.jpg)
Insight Through Computing
Problem Solving Withthe for-Loop
The calculation to be repeated.
end
for count variable
=
expression for starting
value
expression for ending
value:
![Page 3: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/3.jpg)
Insight Through Computing
Question Time
How many lines of output are producedby the following script.
A. 2 B. 50 C. 51 D. 101
for k=100:200 if rem(k,2)~=0 disp(‘k’); endend
![Page 4: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/4.jpg)
Insight Through Computing
There is a line of output for everyOdd number between 100 and 200.
Answer = 50
![Page 5: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/5.jpg)
Insight Through Computing
for count variable
=expression for starting
value
expression for ending
value:
For-Loop Shortcoming
When you use a for-loop, you needto know the exact extent of therepetition.
![Page 6: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/6.jpg)
Insight Through Computing
OK for Many Situations
Here is a typical for-loop Problem:
Simulate the tossing of a fair coin100 times and print the number of“Heads”
![Page 7: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/7.jpg)
Insight Through Computing
% Running sum…
H = 0;
for tosses = 1:100
r = rand;
if r < .5
% Agree that this means “heads”
H = H + 1;
end
end
fprintf(‘H = %2d\n’,H)
![Page 8: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/8.jpg)
Insight Through Computing
Not OK in Other Situations
Simulate the game of “Gap10”:
Toss a fair coin until
| #Heads – #Tails | = 10
Score = number of required tosses
The number of required tossesis not known in advance.
![Page 9: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/9.jpg)
Insight Through Computing
What We Need
A loop that “shuts down’’ as soon as |H-T| = =10.
![Page 10: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/10.jpg)
Insight Through Computing
H = 0; T = 0; tosses = 0;
while abs(H-T)<10
r = rand; tosses = tosses + 1; if r < .5 H = H + 1; else T = T + 1; end
endfprintf( … )
![Page 11: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/11.jpg)
Insight Through Computing
How a While-Loop Works
Warm-up exercise:
Any for-loop can be written as a while-loop.
![Page 12: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/12.jpg)
Insight Through Computing
A Simple For-loop
s = 0;
for k=1:5
s = s + k;
fprintf(‘%2d %2d\n’,k,s))
end1 12 33 6 4 105 15
![Page 13: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/13.jpg)
Insight Through Computing
The While-loop Equivalent
k = 0; s = 0;
while k < 5
k = k + 1; s = s + k;
fprintf(‘%2d %2d\n’,k,s)
end1 12 33 6 4 105 15
![Page 14: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/14.jpg)
Insight Through Computing
How it Works in Detail
k = 0; s = 0;
while k < 5
k = k + 1; s = s + k;
fprintf(‘%2d %2d\n’,k,s)
end
k s
k and s are initialized
![Page 15: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/15.jpg)
Insight Through Computing
How it Works in Detail
k = 0; s = 0;
while k < 5
k = k + 1; s = s + k;
fprintf(‘%2d %2d\n’,k,s)
end
0
k
0
s
Is k < 5 true? Yes. Execute the loop body.
![Page 16: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/16.jpg)
Insight Through Computing
How it Works in Detail
k = 0; s = 0;
while k < 5
k = k + 1; s = s + k;
fprintf(‘%2d %2d\n’,k,s)
end
1
k
1
s
Is k < 5 true? Yes. Execute the loop body.
1 1
![Page 17: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/17.jpg)
Insight Through Computing
How it Works in Detail
k = 0; s = 0;
while k < 5
k = k + 1; s = s + k;
fprintf(‘%2d %2d\n’,k,s)
end
2
k
3
s
Is k < 5 true? Yes. Execute the loop body.
1 1
2 3
![Page 18: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/18.jpg)
Insight Through Computing
How it Works in Detail
k = 0; s = 0;
while k < 5
k = k + 1; s = s + k;
fprintf(‘%2d %2d\n’,k,s)
end
3
k
6
s
Is k < 5 true? Yes. Execute the loop body.
1 1
2 3
3 6
![Page 19: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/19.jpg)
Insight Through Computing
How it Works in Detail
k = 0; s = 0;
while k < 5
k = k + 1; s = s + k;
fprintf(‘%2d %2d\n’,k,s)
end
4
k
10
s
Is k < 5 true? Yes. Execute the loop body.
1 1
2 3
3 6
4 10
![Page 20: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/20.jpg)
Insight Through Computing
How it Works in Detail
k = 0; s = 0;
while k < 5
k = k + 1; s = s + k;
fprintf(‘%2d %2d\n’,k,s)
end
5
k
15
s
Is k < 5 true? No, done with loop.
1 1
2 3
3 6
4 10
5 15
![Page 21: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/21.jpg)
Insight Through Computing
A Modified Problem
Print the smallest k so that
1 + 2 + 3 + … + k >= 30
![Page 22: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/22.jpg)
Insight Through Computing
How it Works in Detail
k = 0; s = 0;
while s < 30
k = k + 1; s = s + k;
end
fprintf(‘%2d %2d\n’,k,s)
k s
k and s are initialized
![Page 23: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/23.jpg)
Insight Through Computing
How it Works in Detail
k = 0; s = 0;
while s < 30
k = k + 1; s = s + k;
end
fprintf(‘%2d %2d\n’,k,s)
0
k
0
s
Is s < 30 true? Yes. Execute loop body.
![Page 24: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/24.jpg)
Insight Through Computing
How it Works in Detail
k = 0; s = 0;
while s < 30
k = k + 1; s = s + k;
end
fprintf(‘%2d %2d\n’,k,s)
1
k
1
s
Is s < 30 true? Yes. Execute loop body.
![Page 25: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/25.jpg)
Insight Through Computing
Defining Variables
k = 0; s = 0;
while s < 30
%s is the sum 1+ … + k
k = k + 1; s = s + k;
end
This “property” is true all during the loop
![Page 26: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/26.jpg)
Insight Through Computing
Spotting a While Situation
InnerA = (n/2) sin(2 /n) Outer A = n tan( /n)
As n increases, InnerA and OuterA approach pi, the area of the unit circle.
When will |OuterA – InnerA| <= .000001?
![Page 27: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/27.jpg)
Insight Through Computing
PseudoCode Development
Repeat while |OuterA – InnerA| >.000001 Increase n Update InnerA Update OuterA
Identify the repetition and a criteria that says “keep iterating”.
![Page 28: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/28.jpg)
Insight Through Computing
PseudoCode Development
n = 3; InnerA = area of inscribed triangleOuterA = area of the circumscribed
triangleRepeat while |OuterA – InnerA| >.000001 Increase n Update InnerA Update OuterA
The “players” have to be initialized
![Page 29: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/29.jpg)
Insight Through Computing
PseudoCode Development
n = 3; InnerA = area of inscribed triangleOuterA = area of the circumscribed
triangleRepeat while |OuterA – InnerA| >.000001 Increase n Update InnerA Update OuterAPrint n
What to do after loop terminates.
![Page 30: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/30.jpg)
Insight Through Computing
Pattern for doing something an Indefinite number of times
% Initialization
while ( not-stopping signal ) % do something
% update status (variables)
end
![Page 31: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/31.jpg)
Insight Through Computing
Question Time
What is the last line of output produced bythis script?
A. 1 B. 2 C. 4 D. 16 E. I dunno
n = 5while n>1 disp(‘I dunno’) if rem(n,2)==0 n = n/2 else n = 3*n+1 endend
![Page 32: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/32.jpg)
Insight Through Computing
Two More While Examples
Each motivated by the limitationsof the for-loop
![Page 33: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/33.jpg)
Insight Through Computing
Example 1: Up/Down Sequence
Pick a random whole number between one and a million. Call the number n andrepeat this process:
if n is even, replace n by n/2. if n is odd, replace n by 3n+1
Does it ever take more than 1000 updates
to reach one?
![Page 34: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/34.jpg)
Insight Through Computing
Aside: Random Integers
How do we generate a randominteger from an interval?
n = ceil(1000000*rand)
![Page 35: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/35.jpg)
Insight Through Computing
Need the Built-In Function ceil
a floor(a) ceil(a)
15.9 15 16
12.0 12 12
floor: next smallest integerceil : next biggest integer
![Page 36: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/36.jpg)
Insight Through Computing
Random Integers
% x is random real, 0 < x < 1
x = rand
% y is random real, 0 < y < 10^6
y = 100000*x
% n is rand integer from 1,…,10^6
n = ceil(y)
n = ceil(1000000*rand)
![Page 37: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/37.jpg)
Insight Through Computing
The Central Repetition:
if rem(n,2)==0
n = n/2;
else
n = 3*n+1
end
Note cycling once n == 1: 1, 4, 2, 1, 4, 2, 1, 4, 2, 1, 4, 2, 1, …
![Page 38: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/38.jpg)
Insight Through Computing
Shuts Down When n==1..
step = 0;while n > 1 if rem(n,2)==0 n = n/2; else n = 3*n + 1; end step = step+1; fprintf(' %4d %7d\n',step,n)end
![Page 39: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/39.jpg)
Insight Through Computing
Cycles after n ==1
for step = 1:1000
if rem(n,2)==0
n = n/2;
else
n = 3*n + 1;
end
fprintf(' %4d %7d\n',step,n)
end
![Page 40: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/40.jpg)
Insight Through Computing
Example 2: Square Roots
Pick a random number x between one and a million. Compute the sqrt(x)
by L = x; W = 1; Repeat until relative error in L <= 10^-
15: L = (L+W)/2; W = x/L; Print relative error in L
![Page 41: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/41.jpg)
Insight Through Computing
Shuts Down After Convergence
s = sqrt(x); L = x; W = 1; k = 0;
while k==0 || relErr > 10^-15
k = k+1;
L = (L+W)/2; W = x/L;
relError = abs(L-s)/s
end
![Page 42: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/42.jpg)
Insight Through Computing
Shuts Down After Convergence
s = sqrt(x); L = x; W = 1; k = 0;
while k==0 || relErr > 10^-15
k = k+1;
L = (L+W)/2; W = x/L;
relError = abs(L-s)/s
end
Error: relErr not initialized when the whileLoop is entered.
![Page 43: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/43.jpg)
Insight Through Computing
Shuts Down After Convergence
s = sqrt(x); L = x; W = 1; k = 0;
while k==0 || relErr > 10^-15
k = k+1;
L = (L+W)/2; W = x/L;
relError = abs(L-s)/s
end
During the first check of the condition, k==0 is true. Matlab doesn’t bother to check the relErr comparison sincethe or is true. No prob that relErr uninitialized
![Page 44: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/44.jpg)
Insight Through Computing
Nested Loop Problem
On average, how many coin tossesare there in a game of Gap10?
Estimate by simulating 10,000 games.
![Page 45: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/45.jpg)
Insight Through Computing
PseudoCode
sum = 0
for k=1:10000
Simulate a game of Gap10 and assign
to the variable tosses the number of required tosses. sum = sum + tosses;
end
p = sum/10000
![Page 46: Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops](https://reader036.vdocument.in/reader036/viewer/2022062318/551b03c5550346f70d8b5789/html5/thumbnails/46.jpg)
Insight Through Computing
H = 0; T = 0; tosses = 0;
while abs(H-T)<10
r = rand;
tosses = tosses + 1;
if r < .5
H = H + 1;
else
T = T + 1;
end
end