14-440-127 Lecture 9 Presentation

14-440-127 Lecture 9 Presentation - 14:440:127 Introduction...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 14:440:127 Introduction to Computers for Engineers Lecture 9 Lecturer: Blase E. Ur Recitation Instructor: Tianming Li Recitation Instructor: Ke Xu Recitation Instructor: Brian Viezbicke Recitation Instructor: Mingliang Wang PLEASE TAKE LECTURE 9 NOTES AS YOU ENTER THE ROOM …Yup, shoulder’s still recovering Rutgers University SOE 14:440:127 Lecture 9 Reminders / Updates • Exam 1- “Hakuna Matata” (<=8% if you do better on Exam 3) • Exam 2- Next Week, beginning on Wednesday (Study Recitation Problems) • Project 2- Yikes, sorry for the delay. Rutgers University SOE 14:440:127 Lecture 9 For Loop for VARIABLE = VECTOR STATEMENTS end for x = 1 : 100 disp(x) end • First time through the loop, x=1 • Then, go back to the top; x=2 • … • The last time through the loop, x=100 Rutgers University SOE 14:440:127 Lecture 9 When Writing…. Find Pattern v(1)=1; %fibonacci v(2)=1; v(3)=v(2)+v(1); v(4)=v(3)+v(2); v(5)=v(4)+v(3); % What is the pattern? v(1)=1; v(2)=1; for x=3:100 v(x)=v(x-1)+v(x-2); end disp(v) Rutgers University SOE 14:440:127 Lecture 9 Sum 1 to 100 with a for loop s = 0; for z = 1:100 s = s+z; end disp(s) % error in notes! Rutgers University SOE 14:440:127 Lecture 9 Sum the odd numbers s = 0; for z = 1: 2 :99 % note the error in your notes s = s+z; end disp(z) Rutgers University SOE 14:440:127 Lecture 9 Sum primes from 1 to 1000 s = 0; for x = 1:1000 if(isprime(x)) s = s+x; end end disp(s) Rutgers University SOE 14:440:127 Lecture 9 While while(CONDITION) % replace capitalized parts STATEMENTS % replace capitalized parts end n=rand(1); while(n<0.5 | n>0.7) n=rand(1); end Rutgers University SOE 14:440:127 Lecture 9 While n=rand(1); count=1; while(n<0.5 | n>0.7) n=rand(1); count=count+1; end disp(n) disp(count) Rutgers University SOE 14:440:127 Lecture 9 While Example: If you sum the integers 1 through n, what is the smallest n for which the sum is a 3 digit number? s = 0; n=1; while(s<100) s = s+n; n=n+1; end fprintf('The smallest n is %.0f',n-1) Rutgers University SOE 14:440:127 Lecture 9 Write a For Loop as a While Loop for x = 1001:2:9997 if(isprime(x) & isprime(x+2)) fprintf('%.0f and %.0f are both prime\n',x,x+2) end end------------------------------------------------------ x=1001; %STEP 1: Set x while(x<=9997) %STEP 2: Write loop if(isprime(x) & isprime(x+2)) fprintf('%.0f and %.0f are both prime\n',x,x+2) end x = x+2; %STEP 3: Last line of loop = increment end Rutgers University SOE...
View Full Document

This note was uploaded on 02/29/2012 for the course 440 127 taught by Professor Blase during the Fall '09 term at Rutgers.

Page1 / 34

14-440-127 Lecture 9 Presentation - 14:440:127 Introduction...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online