{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture14 - Engineering 101 Lecture 14 Analyzing Loops Prof...

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

View Full Document Right Arrow Icon
Engineering 101 Engineering 101 Lecture 14 Lecture 14 Analyzing Loops Analyzing Loops Prof. Michael Falk University of Michigan, College of Engineering
Background image of page 1

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

View Full Document Right Arrow Icon
Announcements Announcements Project 3 program due 10/11, report due 10/12 or 10/13 depending on your section. Exams will be returned this week in section. If you believe anything was graded incorrectly, attach an explanation and turn your exam back to your GSI within one week. Online grades will be updated for your review this week. I will be generating a projected grade for you based on your performance in the first weeks of the class so you can monitor your performance.
Background image of page 2
Second Example: Fibonacci Second Example: Fibonacci // Compute the nth Fibbonaci number // where fib(n)= fib(n-1)+ fib(n-2) double fib(int n){ int a=0, b=1, c, m=1; while (m < n){ c=a+b; a=b; b=c; m=m+1 } return b; b 1 n 5 fib(5) a 0 m 1 c 1, 1, 2, 3, 5, 8, 13, 21, 34, ….
Background image of page 3

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

View Full Document Right Arrow Icon
Second Example: Fibonacci Second Example: Fibonacci // Compute the nth Fibbonaci number double fib(int n){ int a=0, b=1, c, m=1; while (m < n){ c=a+b; a=b; b=c; m = m + 1 } return b; } b 1 n 5 fib(5) a 1 m 2 c 1 1, 1, 2, 3, 5, 8, 13, 21, 34, ….
Background image of page 4
Second Example: Fibonacci Second Example: Fibonacci // Compute the nth Fibbonaci number double fib(int n){ int a=0, b=1, c, m=1; while (m < n){ c=a+b; a=b; b=c; m = m + 1 } return b; } b 2 n 5 fib(5) a 1 m 3 c 2 1, 1, 2, 3, 5, 8, 13, 21, 34, ….
Background image of page 5

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

View Full Document Right Arrow Icon
Second Example: Fibonacci Second Example: Fibonacci // Compute the nth Fibbonaci number double fib(int n){ int a=0, b=1, c, m=1; while (m < n){ c=a+b; a=b; b=c; m = m + 1 } return b; } b 3 n 5 fib(5) a 2 m 4 c 3 1, 1, 2, 3, 5, 8, 13, 21, 34, ….
Background image of page 6
Second Example: Fibonacci Second Example: Fibonacci // Compute the nth Fibbonaci number double fib(int n){ int a=0, b=1, c, m=1; while (m < n){ c=a+b; a=b; b=c; m = m + 1 } return b; } b 5 n 5 fib(5) a 3 m 5 c 5 1, 1, 2, 3, 5, 8, 13, 21, 34, ….
Background image of page 7

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

View Full Document Right Arrow Icon
Second Example: Fibonacci Second Example: Fibonacci // Compute the nth Fibbonaci number double fib(int n){ int a=0, b=1, c, m=1; while (m < n){ c=a+b; a=b; b=c; m = m + 1 } return b; } 5 fib(n) = fib(n-1)+fib(n-2) 1, 1, 2, 3, 5, 8, 13, 21, 34, …. loop invariant: a == fib(m-1) b == fib(m)
Background image of page 8
Second Example: Fibonacci Second Example: Fibonacci // Compute the nth Fibbonaci number double fib(int n){ int a=0, b=1, c, m=1; while (m < n){ c=a+b; a=b; b=c; m = m + 1 } return b; } 5 fib(n) = fib(n-1)+fib(n-2) 1, 1, 2, 3, 5, 8, 13, 21, 34, …. loop invariant: a == fib(m-1) b == fib(m) initialization: m == 1 a == fib(2)-fib(1) b=fib(1) termination: m == n
Background image of page 9

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

View Full Document Right Arrow Icon
Finite Sums Finite Sums What if we want to compute a summation? Sum(m)= Sum(4) = = m n n 1 2 1 .... 4236111 . 1 16 1 9 1 4 1 1 = + + + Write a function to do this.
Background image of page 10
Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0 double sum=0; while (n<m){ sum = sum + 1.0/(m*m); n = n + 1; } return sum; } First we set up the function heading n = m n n 1 2 1
Background image of page 11

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

View Full Document Right Arrow Icon
Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; double sum=0; while (n<m){ sum = sum + 1.0/(m*m); n = n + 1; } return sum; } Include the basic loop machinery n = m n n 1 2 1
Background image of page 12
Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; double sum=0; while (n<m){ sum = sum + 1.0/(m*m); n = n + 1; } return sum; } The rest of the loop body will go here n = m n n 1 2 1
Background image of page 13

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

View Full Document Right Arrow Icon
Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; double sum=0; while (n<m){
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.

{[ snackBarMessage ]}