14-440-127 Lecture 9 Presentation

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

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

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

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

View Full Document
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

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

View Full Document
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!

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

View Full Document
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)

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

View Full Document
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)

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

View Full Document
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

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

View Full Document
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