Top Banner
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions
32

6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

6. More on the For-Loop

Using the Count Variable

Developing For-Loop Solutions

Page 2: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Syntax of the for loop

for <var> = <start value>:<incr>:<end bound>

statements to be executed repeatedly

end

Loop body

Page 3: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Syntax of the for loop

for <var> = <start value>:<incr>:<end bound>

statements to be executed repeatedly

end

Loop header specifies all the values that the index variable will take on, one for each pass of the loop. E.g, k= 3:1:7 means k will take on the values 3, 4, 5, 6, 7, one at a time.

Page 4: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Pattern for doing something n times

n= _____

for k= 1:n

% code to do

% that something

end

Definite iteration

Page 5: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

% What will be printed?for k= 1:2:6

fprintf(‘%d ’, k)end

A: 1 2 3 4 5 6

B: 1 3 5 6

C: 1 3 5

D: error (incorrect bounds)

Page 6: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for loop examples

for k= 2:0.5:3 k takes on the values 2,2.5,3disp(k) Non-integer increment is OK

endfor k= 1:4 k takes on the values 1,2,3,4

disp(k) Default increment is 1endfor k= 0:-2:-6 k takes on the values 0,-2,-4,-6

disp(k) “Increment” may be negativeendfor k= 0:-2:-7 k takes on the values 0,-2,-4,-6

disp(k) Colon expression specifies a boundendfor k= 5:2:1 The set of values for k is the empty

disp(k) set: the loop body won’t executeend

Page 7: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

% What will be printed?for k= 10:-1:14

fprintf(‘%d ’, k)endfprintf(‘!’)

B: 10 (then error)

C: 10 !

D: 14 !

A: error (incorrect bounds)

E: !

Page 8: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

What will be displayed when you run the following script?

for k = 4:6

disp(k)

k= 9;

disp(k)

end

4

9

4

4 Something else …or or

A B C

Page 9: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

k

Output in Command Window

With this loop header, k “promises” to be these values, 1 at a time

Page 10: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

4k

Output in Command Window

Page 11: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

4k

Output in Command Window

4

Page 12: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

4

Page 13: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

49

Page 14: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

5k

Output in Command Window

49

Page 15: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

5k

Output in Command Window

495

Page 16: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

495

Page 17: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

4959

Page 18: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

6k

Output in Command Window

4959

Page 19: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

6k

Output in Command Window

49596

Page 20: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

49596

Page 21: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

495969

Page 22: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

9k

Output in Command Window

495969

Page 23: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

for k = 4:6 disp(k) k= 9; disp(k)end

4 5 6

This is NOT a condition (boolean expression) that checks whether k<=6.

It is an expression that specifies values:

Page 24: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Developing For-Loop Solutions

Illustrate the thinking associated with the design of for-loops

The methodology of stepwise refinement.

An example..

Page 25: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

A Game: TriStick

Pick three sticks each having a randomlength between zero and one.

You win if you can form a trianglewhose sides are the sticks. Otherwiseyou lose.

Page 26: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Win:

Lose:

Page 27: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Problem

Estimate the probability of winninga game of TriStick by simulating a million games and counting the numberof wins.

Page 28: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Pseudocode

Initialize running sum variable.Repeat 1,000,000 times: Play a game of TriStick by picking the three sticks. If you win increment the running sumEstimate the probability of winning

Page 29: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Refine…

% Initialize running sum variable

Wins = 0;

for n = 1:1000000

Play the nth game of TriStick by picking the three sticks. If you win increment the running sum.end

% Estimate the prob of winning

p = wins/1000000

Page 30: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Refine the Loop Body

Play the nth game of TriStick by picking the three sticks.

If you win increment the running sum.

Page 31: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Refine the Loop Body

% Play the nth game of TriStick% by picking the three sticks.a = rand; b = rand; c = rand;

if (a<b+c) && (b<a+c) && (c<a+b) % No stick is longer than the % sum of the other two. wins = wins+1;end

Page 32: 6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Insight Through Computing

Key Problem-Solving Strategy

Progress from pseudocode to Matlabthrough a sequence of refinements.

Comments have an essential role duringthe transitions. They “stay on” all theway to the finished fragment.