{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

08 Loops Full - Engineering101 Loops QuoteoftheDay Voltaire...

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

View Full Document Right Arrow Icon
Click to edit Master subtitle style Engineering 101 Loops
Image of page 1

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

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

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

View Full Document Right 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
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
Image of page 5

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

View Full Document Right 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
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
Image of page 7

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

View Full Document Right 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
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
Image of page 9

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

View Full Document Right 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
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
Image of page 11

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

View Full Document Right 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
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; 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
Image of page 13

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

View Full Document Right 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; 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
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; 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
Image of page 15

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern