lecture09

# lecture09 - CS107 Computing for Math and Science Click to...

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

CS107, Prof. Steinberg, f10 1 Lecture 09 Click to edit Master subtitle style CS107: Computing for Math and Science Lecture 09: More Loops

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

View Full Document
CS107, Prof. Steinberg, f10 2 Lecture 09 Different Parts of a Problem total = 0; for j = 1:n total = total + j end What data to process (numbers 1 through n) What do do with the numbers (add)
CS107, Prof. Steinberg, f10 3 Lecture 09 Different Parts of a Problem Parts of the code correspond to parts of the problem total = 0; for j = 1:n total = total + j end Add

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

View Full Document
CS107, Prof. Steinberg, f10 4 Lecture 09 Different Parts of a Problem Parts of the code correspond to parts of the problem total = 0; for j = 1 : n total = total + j end Numbers 1 to n
CS107, Prof. Steinberg, f10 5 Lecture 09 Related Problems Problem: Add the numbers from 1 to n Related problems Add the numbers in a vector Multiply the numbers from 1 to n Multiply the numbers in a vector

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

View Full Document
CS107, Prof. Steinberg, f10 6 Lecture 09 total = 0; for j = 1: length(data) total = total + data(j) end total = 0; for j = 1: n total = total + j end Mix and Match prod = 1; for j = 1: length(data) prod = prod .* data(j) end prod = 1; for j = 1: n prod = prod .* j end Add Multiply Data in vector Numbers 1 to n
CS107, Prof. Steinberg, f10 7 Lecture 09 total = 0; for j = 1: n total = total + j .^2; end total = 0; for j = 1: 5 total = total + input(‘number? ‘); end Mix and Match prod = 1; for j = 1: n prod = prod .* j .^2; end prod = 1; for j = 1: 5 total = total .* input(‘number? ‘); end Add Multiply Squares of 1 to n 5 input numbers

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

View Full Document
CS107, Prof. Steinberg, f10 8 Lecture 09 Finding Largest Summary variable: largest so far Eg biggest Update: if new > biggest biggest = new end Initialize to a number <= final result
CS107, Prof. Steinberg, f10 9 Lecture 09 Debugging Loops EG find first space: None of what we loop thru: no chars One of what we loop thru : one space Most possible of something: not applicable Thing you are looking for In first place ‘ xy’ In last place ‘xy ‘ In the only place ‘ ‘ Not present ‘abc’ More than one ‘f g h’

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

View Full Document
CS107, Prof. Steinberg, f10 10 Lecture 09 Find Pairs function findPairs(numbers) for j = 1:length(numbers) - 1 for k = j+1 : length(numbers) if numbers(j) == numbers(k) disp([numbers(j) j k] ) end end end
CS107, Prof. Steinberg, f10

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