{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L6post

# L6post - Previous Lecture Iteration using for Todays...

This preview shows pages 1–7. Sign up to view the full content.

Previous Lecture: Iteration using for Today’s Lecture: Detail on the for -loop Iteration using while Review loops, conditionals using graphics Announcements: Project 2 posted, due Thursday, 9/24 We do not use break in this course Discussion section takes place in the classrooms , not lab, this week

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
September 15, 2009 Lecture 6 3 Monte Carlo π with N darts on L-by-L board hits = 0; for k = 1:N % Throw kth dart x = rand(1)*L – L/2; y = rand(1)*L – L/2; % Count it if it is in the circle if sqrt(x^2+y^2) <= L/2 hits = hits + 1; end end myPi = 4*hits/N;
September 15, 2009 Lecture 6 4 for loop examples for k= 2:0.5:3 k takes on the values 2,2.5,3 disp(k) Non-integer increment is OK end for k= 1:4 k takes on the values 1,2,3,4 disp(k) Default increment is 1 end for k= 0:-2:-6 k takes on the values 0,-2,-4,-6 disp(k) “Increment” may be negative end for k= 0:-2:-7 k takes on the values 0,-2,-4,-6 disp(k) Colon expression specifies a bound end for k= 5:2:1 disp(k) end

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
September 15, 2009 Lecture 6 6 for loop examples for k= 2:0.5:3 k takes on the values 2,2.5,3 disp(k) Non-integer increment is OK end for k= 1:4 k takes on the values 1,2,3,4 disp(k) Default increment is 1 end for k= 0:-2:-6 k takes on the values 0,-2,-4,-6 disp(k) “Increment” may be negative end for k= 0:-2:-7 k takes on the values 0,-2,-4,-6 disp(k) Colon expression specifies a bound end for k= 5:2:1 The set of values for k is the empty disp(k) set: the loop body won’t execute end
September 15, 2009 Lecture 6 7 % What will be printed? for k= 10:-1:14 fprintf(‘%d ’, k) end fprintf(‘!’) B: 10 (then error) C: 10 ! D: 14 ! A: error (incorrect bounds) E: !

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
September 15, 2009 Lecture 6 8 What will be displayed when you run the following script?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern