{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

mylecture08

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

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

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

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

View Full Document
CS107, Prof. Steinberg, f10 2 Lecture 08 Input The function input(prompt) prints the prompt, waits for user to type data, returns that data. E.g. num1 = input(‘type first number ‘); num2 = input(‘type second number ‘); sum = num1 + num2;
CS107, Prof. Steinberg, f10 3 Lecture 08 Input vs Argument Input Function argument Data come from The user The program When At any time When the call starts

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

View Full Document
CS107, Prof. Steinberg, f10 4 Lecture 08 Input vs Argument
CS107, Prof. Steinberg, f10 5 Lecture 08 Output vs Result Output Function result Data goes to The user The program When At any time When the call ends

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

View Full Document
CS107, Prof. Steinberg, f10 6 Lecture 08 Loops To repeat the same lines of code over and over, you can use a for statement for j = [1:3] disp(j) end This Sets j to 1 and does the disp statement (prints 1) Sets j to 2 and does the disp statement (prints 2) Sets j to 3 and does the disp statement (prints 3) Continues with statements after the end
CS107, Prof. Steinberg, f10 7 Lecture 08 Loops What do these print for k = [1 4 5 2] disp(k); end m = 8; for n = [1 : 3] disp(m + n) end

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

View Full Document
CS107, Prof. Steinberg, f10 8 Lecture 08 Example Do something with the numbers, e.g. store them in a vector for j = [1:4] data = input(‘type a number ‘); numbers(j) = data end
CS107, Prof. Steinberg, f10 9 Lecture 08 Summary Variables Solution: keep a running total Need a variable to keep the running total sum = 0; initialize before the loop for j = 1:4 number = input(‘type a number ‘); sum = sum + number; update inside loop end Summary variable is a synonym for accumulator

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

View Full Document
CS107, Prof. Steinberg, f10 10 Lecture 08 Example: sum to n sumToN(n): 1 + 2 + 3 +… + n You write it What should the initial value of the summary variable be? How should we update the summary variable?
CS107, Prof. Steinberg, f10 11 Lecture 08 Example We don’t always need a summary variable in a loop Suppose I record the number of students who attend recitation each week Eg [25 30 29 40 15 26] And I want to know the change from week to week Eg [5 -1 11 -25 11]

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

View Full Document
CS107, Prof. Steinberg, f10 12 Lecture 08 Example for j = [1 : length(attendees) - 1] change(j) = attendees(j +1) - attendees(j); end j 1 attendees 25 30 29 40 15 26 1 2 3 4 5 6 attendees(j ) attendees(j+1) `
CS107, Prof. Steinberg, f10 13 Lecture 08 Example for j = [1 : length(attendees) - 1] change(j) = attendees(j +1) - attendees(j); end j 2 attendees 25 30 29 40 15 26 1 2 3 4 5 6 attendees(j ) attendees(j+1) `

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

View Full Document
CS107, Prof. Steinberg, f10
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 53

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

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

View Full Document
Ask a homework question - tutors are online