{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

Engineering 101 Engineering 101 Lecture 14 Lecture 14 Analyzing Loops Analyzing Loops Prof. Michael Falk University of Michigan, College of Engineering

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

View Full Document
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.
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, ….

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

View Full Document
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, ….
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, ….

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

View Full Document
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, ….
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, ….

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

View Full Document
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)
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

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

View Full Document
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.
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

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

View Full Document
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
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

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

View Full Document
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){
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}