08+-+Loops+-+Full

# 08+-+Loops+-+Full - Engineering101 Loops QuoteoftheDay...

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

Engineering 101 Loops

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

View Full Document
Quote of the Day Common sense is not so common. - Voltaire
Finite Sums n 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.

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

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

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

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

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

View Full Document
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? 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; while (n<m){ n = n + 1; } } Did I have to set the loop up this way? 0 1 2 3 4 . . . m-1 m = m n n 1 2 1

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

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

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

View Full Document
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; } } Let’s say I chose the first one. What would my loop body look like? 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; } return sum; } I have to use (n+1) in the body because the first time through n=0. 0 1 2 3 4 . . . m-1 = m n n 1 2 1

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

View Full Document
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; } return sum; } The loop invariant which holds after each time the body is completed is: 0 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; } return sum; } At the end when n==m I have found the answer! 0 1 2 3 4 . . . m-1 = = n p p sum 1 2 1 = m n n 1 2 1

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

View Full Document
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; } return sum; } What if we chose the other way? 0
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 57

08+-+Loops+-+Full - Engineering101 Loops QuoteoftheDay...

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

View Full Document
Ask a homework question - tutors are online