Lecture+11 - Engineering 101 Engineering 101/9/07 More...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Engineering 101 Lecture 11 10/9/07 More Loops Quote of the Day Quote of the Day Change is inevitable. Stupidity is a choice. ­ Joe Ringenberg Common sense is not so common. ­ Voltaire Finite Sums Finite Sums What if we want to compute a summation? Sum(m)= 1 ∑ n2 n =1 111 1 + + + = 1.4236111.... 4 9 16 m Sum(4) = Write a function to do this. Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ 1 ∑ n2 n =1 First we set up the function heading m } Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 1 ∑ n2 n =1 Include the basic loop machinery m while (n<m){ } } n = n + 1; Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 1 ∑ n2 n =1 The rest of the loop body will go here m while (n<m){ } } n = n + 1; Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 1 ∑ n2 n =1 What values will n take in the body as the loop executes? m while (n<m){ } } n = n + 1; Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 0 1 2 while (n<m){ 3 4 . . n = n + 1; . } m-1 } 1 ∑ n2 n =1 What is the value of n after the loop is done? m Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 0 1 2 while (n<m){ 3 4 . . n = n + 1; . } m-1 } m 1 ∑ n2 n =1 Did I have to set the loop up this way? m Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 0 1 2 while (n<m){ 3 4 . . n = n + 1; . } m-1 } m 1 ∑ n2 n =1 NO! I could have started at 1 and gone to m m Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=1; 1 2 3 while (n<=m){ 4 5 . . n = n + 1; . } m } m+1 1 ∑ n2 n =1 NO! I could have started at 1 and gone to m m Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 0 1 2 while (n<m){ 3 4 . . n = n + 1; . } m-1 } 1 ∑ n2 n =1 Let’s say I chose the first one. What would my loop body look like? m Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 0 double sum=0; 1 2 while (n<m){ 3 sum = sum + 4 1.0/((n+1)*(n+1)); . . n = n + 1; . } m-1 return sum; } 1 ∑ n2 n =1 I have to use (n+1) in the body because the first time through n=0. m Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 0 double sum=0; 1 2 while (n<m){ 3 sum = sum + 4 1.0/((n+1)*(n+1)); . . n = n + 1; . } m-1 return sum; } 1 ∑ n2 n =1 The loop invariant which holds after each time the body is completed is: m sum = ∑ p =1 n 1 2 p Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 0 double sum=0; 1 2 while (n<m){ 3 sum = sum + 4 1.0/((n+1)*(n+1)); . . n = n + 1; . } m-1 return sum; } 1 ∑ n2 n =1 At the end when n==m I have found the answer! m sum = ∑ p =1 n 1 2 p Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 0 double sum=0; 1 2 while (n<m){ 3 sum = sum + 4 1.0/((n+1)*(n+1)); . . n = n + 1; . } m-1 return sum; } 1 ∑ n2 n =1 What if we chose the other way? m sum = ∑ p =1 n 1 2 p Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ Four things int n=1; 1 change: double sum=0; 2 initialization, 3 termination condition, while (n<=m){ 4 body sum = sum + and invariant. 5 1.0/(n*n); . n = n + 1; . n −1 } . return sum; m 2 } 1 ∑ n2 n =1 m sum = ∑ p =1 1 p Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; double sum=0; termination: while (n<m){ n == m sum = sum + 1.0/((n+1)*(n+1)); n = n + 1; } return sum; } 1 ∑ n2 n =1 initialization: n == 0 sum == 0 m loop invariant: sum = ∑p p =1 n .1 2 Finite Sums Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=1; double sum=0; termination: while (n<=m){ n == m+1 sum = sum + 1.0/(n*n); n = n + 1; } return sum; } 1 ∑ n2 n =1 initialization: n == 1 sum == 0 m loop invariant: sum = ∑p p =1 n −1 .1 2 Not All Loops Count Not All Loops Count For example you could have a loop that prompts a user for input until it gets a number between 1 and 10: cout << “input a number from 1 to 10:” << endl; cin >> x; while (x<1 or x > 10){ cout << “Outside range! Try again:” << endl; cin >> x; } Simple Counting Loops Simple Counting Loops Many of the loops we have looked at so far are of the form: n = 0; while (n<N){ do something n = n + 1; } Simple Counting Loops Simple Counting Loops Many of the loops we have looked at so far are of the form: n = 0; while (n<N){ do something n = n + 1; } This loop counts from 0 to N-1, but if we wanted to we could start at 5 and count by 3’s. Simple Counting Loops Simple Counting Loops Many of the loops we have looked at so far are of the form: n = 5; while (n<N){ do something n = n + 3; } This loop counts from 0 to N-1, but if we wanted to we could start at 5 and count by 3’s. Simple Counting Loops Simple Counting Loops This kind of loop is so common it has a short­hand form, the “for” loop: Continuation n = 5; while (n<N){ do something n = n + 3; } Initialization Counter for(n = 5; n<N; n = n + 3){ do something } Simple Counting Loops Simple Counting Loops Commas can be used to include multiple initializations or counters i=1; n = 5; while (n<N){ do something i = i*n; n = n + 3; } Initialization Continuation Counter for(n = 5, i=1; n<N; n = n + 3){ do something i = i*n; } Simple Counting Loops Simple Counting Loops Commas can be used to include multiple initializations or counters i=1; n = 5; while (n<N){ do something i = i*n; n = n + 3; } Initialization Continuation Counter for(n = 5, i=1; n<N; i=i*n, n = n + 3){ do something } Exercise Exercise Convert this code to a for loop: int n = 10, x = 0; while(n >= 1){ x = x + n; n = n + 1; } 1­ for(n=10, x=0; n>=1; x=x+n) n=n+1; 2­ for(n=10, x=0, n>=1, n=n+1) x=x+n; 3­ for(n=10, x=0; n>=1; x=x+n) n=n+1; 4­ for(n=10, x=0; n>=1; n=n+1) x=x+n; Exercise Exercise Convert this code to a for loop: int n = 10, x = 0; while(n >= 1){ x = x + n; n = n + 1; } 1­ for(n=10, x=0; n>=1; x=x+n) n=n+1; 2­ for(n=10, x=0, n>=1, n=n+1) x=x+n; 3­ for(n=10, x=0; n>=1; x=x+n) n=n+1; 4­ for(n=10, x=0; n>=1; n=n+1) x=x+n; News Flash! Choose your own topics! Use those laptops List and vote: 1. 2. 3. Increment/ Decrement Increment/ Decrement Operators The operator ++ increments by 1 and assigns. When placed after the identifier. a++; is the same as a=a+1; The operator ­­ decrements by 1 and assigns. When placed after the identifier. a­­; is the same as a=a­1; Loops inside loops are called “nested” loops. Example: multiplication tables 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 How would we print this? Loops Can Reside Inside Other Loops Can Reside Inside Other Loops Loops inside loops are called “nested” loops. Example: multiplication tables 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 1 x 3 = 3 2 x 3 = 6 3 x 3= 9 On each line the 2nd number is the same Loops Can Reside Inside Other Loops Can Reside Inside Other Loops Loops inside loops are called “nested” loops. Example: multiplication tables 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 But the 1st number is changing Loops Can Reside Inside Other Loops Can Reside Inside Other Loops Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 int a, b; 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 At the center of our loop is the cout statement. cout << a << “ x ” << b << “ = ” << a*b << “ ”; Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 int a, b; 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 On each line the first number ranges from 1 to 3 a=1, a=2 and a=3. for(a=1; a<=3; a++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 int a, b; 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 After each line we want to skip to the next line for(a=1; a<=3; a++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 Now the second number has int a, b; to change on each line for(b=1; b<=3; b++){ b=1, b=2 and b=3. for(a=1; a<=3; a++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; } Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 Why did we have to put int a, b; the “a” loop inside for(b=1; b<=3; b++){ the “b” loop? for(a=1; a<=3; a++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; } Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 The inside loop always int a, b; changes more frequently. for(b=1; b<=3; b++){ “a” changes more frequently. for(a=1; a<=3; a++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; } Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 If we changed the order of int a, b; the loop we would change for(b=1; b<=3; b++){ the format of the table. for(a=1; a<=3; a++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; } Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 If we changed the order of int a, b; the loop we would change for(a=1; a<=3; a++){ the format of the table. for(b=1; b<=3; b++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; } Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 int a, b; Now “b”, the second value, for(a=1; a<=3; a++){ changes more frequently. for(b=1; b<=3; b++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; } Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 The above table is redundant int a, b; For example it includes for(a=1; a<=3; a++){ 2x1 and 1x2. for(b=1; b<=3; b++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; } Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 int a, b; We can eliminate the upper for(a=1; a<=3; a++){ triangle by varying the loop for(b=1; b<=3; b++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; } Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 Instead of b counting from int a, b; b=1 to b=3, b counts from for(a=1; a<=3; a++){ b=1 to b=a. for(b=1; b<=3; b++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; } Loops Can Reside Inside Other Loops Can Reside Inside Other Loops 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 2 x 2 = 4 3 x 2 = 6 3 x 3 = 9 Instead of b counting from int a, b; b=1 to b=3, b counts from for(a=1; a<=3; a++){ b=1 to b=a. for(b=1; b<=a; b++){ cout << a << “ x ” << b << “ = ” << a*b << “ ”; } cout << endl; } Which of the programs produces the following output? 1321 2321 3321 Exercise Exercise Which of the programs produces the following output? 1321 2321 3321 Exercise Exercise Which is the output of number 2? 1 1321 2321 3321 Exercise Exercise 2 13 2 1 23 2 1 33 2 1 3 31 21 11 32 22 12 33 23 13 4 13 12 11 23 22 21 33 32 31 Which is the output of number 2? 1 1321 2321 3321 Exercise Exercise 2 13 2 1 23 2 1 33 2 1 3 31 21 11 32 22 12 33 23 13 4 13 12 11 23 22 21 33 32 31 Write a predicate to determine if a number is prime. What would the header look like? Loops with More Complicated Loops with More Complicated Conditionals: Primes Write a predicate to determine if a number is prime. What would the header look like? bool prime( int n) Loops with More Complicated Loops with More Complicated Conditionals: Primes Loops with More Complicated Loops with More Complicated Conditionals: Primes Write a predicate to determine if a number is prime. bool prime( int n){ int p=2; while ((p<n) and (n%p!=0)) p = p + 1; return p>=n; } Loops with More Complicated Loops with More Complicated Counters: Primes What if we want to find the n­th prime? int nthprime(int n) Recall that for the computer True == 1 and False == 0 Use prime(x) Loops with More Complicated Loops with More Complicated Counters: Primes What if we want to find the n­th prime? int nthprime(int n){ int m=0, p=1; while (m<n){ p=p+1; m = m + prime(p); } return p; } Pre­Project 4 Info Pre­Project 4 Info Random Walks Start at some point The distance between two adjacent points is a constant Each step is chosen at random with equal probability One­Dimensional Walks One­Dimensional Walks Two­Dimensional Walks Two­Dimensional Walks Three­Dimensional Walks Three­Dimensional Walks Pre­Project 4 Info Pre­Project 4 Info Some Interesting Facts One and Two­Dimensional random walks are recurrent Three and More­Dimensional random walks are transient Random walks are used to: Estimate the size of the World Wide Web Describe the statistical properties of genetic drift Model gambling Next Lecture Next Lecture Selection ...
View Full Document

This note was uploaded on 03/12/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Fall '07 term at University of Michigan.

Ask a homework question - tutors are online