Lecture14 - Engineering 101 Engineering 101 Lecture 14...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Engineering 101 Engineering 101 Lecture 14 Lecture 14 Analyzing Loops Analyzing Loops Prof. Michael Falk University of Michigan, College of Engineering 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 m 1 c 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 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, . 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, . 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 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 Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2)...
View Full Document

This note was uploaded on 04/02/2008 for the course ENGR 101 taught by Professor Ringenberg during the Fall '07 term at University of Michigan.

Page1 / 61

Lecture14 - Engineering 101 Engineering 101 Lecture 14...

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

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