08 Loops Full - Click to edit Master subtitle style...

Info iconThis preview shows pages 1–16. 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

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: Click to edit Master subtitle style Engineering 101 Loops Quote of the Day Common sense is not so common.- Voltaire Finite Sums n What if we want to compute a summation? Sum(m)= = m n n 1 2 1 .... 4236111 . 1 16 1 9 1 4 1 1 = + + + Write a function to do this. Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ First we set up the function heading = m n n 1 2 1 Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; while (n<m){ n = n + 1; Include the basic loop machinery = m n n 1 2 1 Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; while (n<m){ n = n + 1; The rest of the loop body will go here = m n n 1 2 1 Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; while (n<m){ n = n + 1; What values will n take in the body as the loop executes? = m n n 1 2 1 Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; while (n<m){ n = n + 1; What is the value of n after the loop is done? 1 2 3 4 . . . m-1 = m n n 1 2 1 Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; while (n<m){ n = n + 1; Did I have to set the loop up this way? 1 2 3 4 . . . m-1 m = m n n 1 2 1 Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; while (n<m){ n = n + 1; NO! I could have started at 1 and gone to m 1 2 3 4 . . . m-1 m = m n n 1 2 1 Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=1 ; while (n<=m){ n = n + 1; NO! I could have started at 1 and gone to m 1 2 3 4 5 . . . m m+1 = m n n 1 2 1 Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; while (n<m){ n = n + 1; Lets say I chose the first one. What would my loop body look like? 1 2 3 4 . . . m-1 = m n n 1 2 1 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/((n+1)*(n+1)); n = n + 1; I have to use (n+1) in the body because the first time through n=0. 1 2 3 4 . . . m-1 = m n n 1 2 1 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/((n+1)*(n+1)); n = n + 1; The loop invariant which holds after each time the body is completed is: 1 2 3 4 . . . m-1 = = n p p sum 1 2 1 = m n n 1 2 1 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/((n+1)*(n+1)); n = n + 1; At the end when n==m I have found the answer!...
View Full Document

Page1 / 59

08 Loops Full - Click to edit Master subtitle style...

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

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