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

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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)
Background 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
Background image of page 3

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

View Full DocumentRight 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
Background 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
Background image of page 5

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

View Full DocumentRight 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
Background 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
Background image of page 7

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

View Full DocumentRight 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
Background 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’
Background image of page 9

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

View Full DocumentRight 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
Background image of page 10
Background image of page 11

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

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

This note was uploaded on 02/10/2011 for the course COMPUTER S 107 taught by Professor Imielinizki during the Spring '11 term at Rutgers.

Page1 / 42

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

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