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

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

View Full Document Right Arrow Icon
Engineering 101 Loops
Background image of page 1

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

View Full DocumentRight Arrow Icon
Quote of the Day Common sense is not so common. - Voltaire
Background image of page 2
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.
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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
Background image of page 11

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

View Full DocumentRight Arrow Icon
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
Background image of page 12
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
Background image of page 13

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

View Full DocumentRight Arrow Icon
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
Background image of page 14
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
Background image of page 15

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

View Full DocumentRight Arrow Icon
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
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online