More on Iteration Using "For" - Notes | CS 1112, Study notes of Computer Science

Material Type: Notes; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2009;

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-b5r
koofers-user-b5r 🇺🇸

9 documents

1 / 39

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Previous Lecture:
Branching
Logical operators and values
Introduction to for-loop
Today’s Lecture:
More on iteration using for
Announcements
Register your clicker!
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27

Partial preview of the text

Download More on Iteration Using "For" - Notes | CS 1112 and more Study notes Computer Science in PDF only on Docsity!

„^ Previous Lecture:^ „^ Branching^ „^ Logical operators and values^ „^ Introduction to

for-loop

„^ Today’s Lecture:^ „^ More on iteration using

for

„^ Announcements^ „^ Register your clicker!

February 3, 3009^

Lecture 5^

Question

A stick of unit length is split into two pieces. Thebreakpoint is randomly selected. On average, howlong is the shorter piece?

February 3, 3009^

Lecture 5^

n= 10000;^

% number of trials

total= 0;^

% accumulated length so far

for^ k= 1:n% one trial of the experimentbreakPt= rand(1);shortPiece= min(breakPt, 1-breakPt);total= total + shortPiece;end aveLength= total/nfprintf(‘Average length is %f\n’, ...

aveLength)

February 3, 3009^

Lecture 5^

Example: “Accumulate” a solution %^ Average^10 numbers

from^ user^ input n=^ 10;^ %^ number

of^ data^ values for^ k=^ 1:n%^ read^ and^ process

input^ value num=^ input('Enter

a^ number:^ '); total=^ total^ + num; endave=^ total/n;^

%^ average^ of^ n numbers fprintf('Average

is^ %f\n',^ ave)

How many passesthrough the loop willbe completed?^ A:^0 B:^1 C:^9 D:^10 E:^11

February 3, 3009^

Lecture 5^

Important Features of Iteration „^ A task can be accomplished if some steps arerepeated; these steps form the loop body „^ Need a starting point „^ Need to know when to stop „^ Need to keep track of (and measure) progress—update

February 3, 3009^

Lecture 5^

Monte Carlo Estimation of

π^ Sq. area =

ThrowN^ darts^ N^ =^ L^ ×^ L Circle area =^ Nin^2^ =^ π^ L^ /

L^
L/

February 3, 3009^

Lecture 5^

Monte Carlo Approximation of PiFor each of N trialsThrow a dartIf it lands in circleadd^

1 to total # of hitsPi is 4*hits/N

February 3, 3009^

Lecture 5^

Monte Carlo Pi with N darts on L-by-L board for k = 1:N endmyPi = 4*hits/N;

February 3, 3009^

Lecture 5^

Monte Carlo Pi with N darts on L-by-L board for k = 1:N% Throw kth dartx = rand(1)L – L/2;y = rand(1)L – L/2;% Is it in the circle?endmyPi = 4*hits/N;

February 3, 3009^

Lecture 5^

Monte Carlo Pi with N darts on L-by-L board for k = 1:N% Throw kth dartx = rand(1)L – L/2;y = rand(1)L – L/2;% Is it in the circle?if sqrt(x^2+y^2) <= L/2hits = hits + 1;endendmyPi = 4*hits/N;

February 3, 3009^

Lecture 5^

Syntax of the

for^ loop

for^ < var >= < start value

>:< incr >:< end bound

statements to be executed repeatedly end

Loop body

February 3, 3009^

Lecture 5^

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

February 3, 3009^

Lecture 5^

% What will be printed?for k= 1:2:6fprintf(‘%d ’, k)end

A:^ 1 2 3 4 5 6B:^ 1 3 5 6C:^ 1 3 5 D:error(incorrect bounds)

February 3, 3009^

Lecture 5^

for^ loop examples
for k= 2:0.5:
k^ takes on the values 2,2.5,3 disp(k) Non-integer increment is OK
endfor k= 1:^
k^ takes on the values 1,2,3,
disp(k)^
Default increment is 1
endfor k= 0:-2:-
k^ takes on the values 0,-2,-4,-6 disp(k) “Increment” may be negative
endfor k= 0:-2:-
k^ takes on the values 0,-2,-4,-6 disp(k) Colon expression specifies a
bound
endfor k= 5:2:
The set of values for k is the empty disp(k) set: the loop body won’t execute
end