{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08 Loops Full

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

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

Click to edit Master subtitle style 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)= = 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; 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; 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; 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; 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 ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern