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

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

View Full Document Right Arrow Icon
CS107, Prof. Steinberg, f10 1 Lecture 09 Click to edit Master subtitle style CS107: Computing for Math and Science Lecture 09: More Loops
Image of page 1

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

View Full Document Right Arrow Icon
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)
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
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
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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
Image of page 8
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’
Image of page 9

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

View Full Document Right Arrow Icon
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
Image of page 10
CS107, Prof. Steinberg, f10
Image of page 11

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern