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

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

View Full Document Right Arrow Icon
CS107, Prof. Steinberg, f10 1 Lecture 08 Click to edit Master subtitle style CS107: Computing for Math and Science Lecture 08: 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 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;
Background image of page 2
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
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 08 Input vs Argument
Background image of page 4
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
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 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
Background image of page 6
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
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 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
Background image of page 8
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
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 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?
Background image of page 10
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]
Background image of page 11

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

View Full DocumentRight Arrow Icon
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) `
Background image of page 12
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) `
Background image of page 13

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

View Full DocumentRight Arrow Icon
CS107, Prof. Steinberg, f10
Background image of page 14
Image of page 15
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 / 53

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online