This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Engineering 101 Quote of the Day Common sense is not so common.
 Voltaire Loops Finite Sums
What if we want to compute a summation? Finite Sums
// Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ m n =1 n 1
2 m
Sum(m)= n
n =1 1
2
First we set up the function heading Sum(4) = 1+ 1 1 1 + + = 1.4236111.... 4 9 16
} Write a function to do this. Finite Sums
// Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 1 n2 n =1
m
Include the basic loop machinery Finite Sums
// Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 1 n2 n =1
m
The rest of the loop body will go here while (n<m){ while (n<m){ n = n + 1; } } } } n = n + 1; 1 Finite Sums
// Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; 1 n2 n =1
m
What values will n take in the body as the loop executes? 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; . . } m1 } m n =1 n 1
2 while (n<m){ What is the value of n after the loop is done? n = n + 1; } } 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; . . } m1 } m 1 n2 n =1
m
Did I have to set the loop up this way? 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; . . } m1 } m m n =1 n 1
2 NO! I could have started at 1 and gone to m Finite Sums
// Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=1; n=1; 1 2 3 while (n<=m){ 4 5 . n = n + 1; . . } m } m+1 1 n2 n =1
m
NO! I could have started at 1 and gone to m 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; . . } m1 } 1 n2 n =1
m
Let's say I chose the first one. What would my loop body look like? 2 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; . . } m1 return sum; } 1 n2 n =1
m
I have to use (n+1) in the body because the first time through n=0. 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; . . } m1 return sum; } m n =1 n 1
2 The loop invariant which holds after each time the body is completed is: n sum = p =1 1 p2 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; . . } m1 return sum; } 1 n2 n =1
m
At the end when n==m I have found the answer! 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; . . } m1 return sum; } m n =1 n 1
2 What if we chose the other way? n sum = p =1 1 p2 n sum = p =1 1 p2 Finite Sums // Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ Four things int n=1; n=1; 1 change: double sum=0; 2 initialization, 3 termination condition, while (n<=m){ n<=m){ 4 body sum = sum + and invariant. 5 1.0/(n*n); 1.0/(n*n); . n = n + 1; . n 1 } . return sum; m 2 } 1 n2 n =1
m Finite Sums
// Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=0; double sum=0; m n =1 n
n == 0 1
2 initialization: termination: sum = p =1 1 p while (n<m){ n == m sum = sum + 1.0/((n+1)*(n+1)); n = n + 1; } return sum; } sum == 0 loop invariant: sum = .p
p =1 n 1
2 3 Finite Sums
// Return the Sum (n=1 to m) 1/(n^2) double Sum(int m){ int n=1; double sum=0; m n =1 n
n == 1 1
2 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: 10: cout << "input a number from 1 to 10:" << endl; endl; cin >> x; while (x<1 or x > 10){ cout << "Outside range! Try again:" << endl; endl; cin >> x; } initialization: termination: while (n<=m){ n == m+1 sum = sum + 1.0/(n*n); n = n + 1; } return sum; } sum == 0 loop invariant: sum = .p
p =1 n 1 1
2 Simple Counting Loops
Many of the loops we have looked at so far are of the form: 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; } n = 0; while (n<N){ do something n = n + 1; } This loop counts from 0 to N1, but if we wanted to we could start at 5 and count by 3's. Simple Counting Loops
Many of the loops we have looked at so far are of the form: Simple Counting Loops
This kind of loop is so common it has a shorthand form, the short"for" loop: Continuation n = 5; while (n<N){ do something n = n + 3; } This loop counts from 0 to N1, but if we wanted to we could start at 5 and count by 3's. n = 5; while (n<N){ do something n = n + 3; } Initialization Counter for(n = 5; n<N; n = n + 3){ do something } 4 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 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 Continuation Counter for(n = 5, i=1; n<N; n = n + 3){ do something i = i*n; } for(n = 5, i=1; n<N; i=i*n, n = n + 3){ do something } Exercise
Convert this code to a forloop: forloop: int n = 10, x = 0; while(n >= 1){ x = x + n; n = n + 1; } A note on reading date from a file
The extraction operator parses input based on blocks of whitespace (tabs, spaces, newlines) Reading past the end of a file will cause the input stream to go into a failed state
No new input values for variables can be read They retain their old values 1 for(n=10; x=0; n>=1; x=x+n) n=n+1; x=x+n) n=n+1; 2 for(n=10, x=0, n>=1, n=n+1) x=x+n; x=x+n; 3 for(n=10, x=0; n>=1; x=x+n) n=n+1; x=x+n) 4 for(n=10, x=0; n>=1; n=n+1) x=x+n; x=x+n; You can't read data from a failed input stream Example: What's the screen output?
ifstream in("in.txt"); int x, n; for (n=0;n<8;n++) { in >> x; cout << x; }
1 3 5 2 4 6
"in.txt" Loops Can Reside Inside Other Loops
Loops inside loops are called "nested" loops. Example: multiplication tables 1x1=1 2x1=2 3x1=3 1x2=2 2x2=4 3x2=6 1x3=3 2x3=6 3x3=9 How would we print this? 7 1) 2) 3) 4) 127 1273456 12734566 56 5 Loops Can Reside Inside Other Loops
Loops inside loops are called "nested" loops. Example: multiplication tables 1x1=1 2x1=2 3x1=3 1x2=2 2x2=4 3x2=6 1x3=3 2x3=6 3x3=9 On each line the 2nd number is the same Loops Can Reside Inside Other Loops
Loops inside loops are called "nested" loops. Example: multiplication tables 1x1=1 2x1=2 3x1=3 1x2=2 2x2=4 3x2=6 1x3=3 2x3=6 3x3=9 But the 1st number is changing Loops Can Reside Inside Other Loops
1x1=1 1x2=2 1x3=3
int a, b; Loops Can Reside Inside Other Loops
1x1=1 1x2=2 1x3=3
int a, b; 2x1=2 2x2=4 2x3=6 3x1=3 3x2=6 3x3=9 2x1=2 2x2=4 2x3=6 3x1=3 3x2=6 3x3=9 At the center of our loop is the cout statement. On each line the first number ranges from 1 to 3 a=1, a=2 and a=3. cout << a << " x " << b << " = " << a*b << " "; for(a=1; a<=3; a++){ cout << a << " x " << b << " = " << a*b << " "; } Loops Can Reside Inside Other Loops
1x1=1 1x2=2 1x3=3
int a, b; Loops Can Reside Inside Other Loops
1x1=1 1x2=2 1x3=3 2x1=2 2x2=4 2x3=6 3x1=3 3x2=6 3x3=9 2x1=2 2x2=4 2x3=6 3x1=3 3x2=6 3x3=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; 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; } 6 Loops Can Reside Inside Other Loops
1x1=1 1x2=2 1x3=3 2x1=2 2x2=4 2x3=6 3x1=3 3x2=6 3x3=9 Loops Can Reside Inside Other Loops
1x1=1 1x2=2 1x3=3 2x1=2 2x2=4 2x3=6 3x1=3 3x2=6 3x3=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; } 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
1x1=1 1x2=2 1x3=3 2x1=2 2x2=4 2x3=6 3x1=3 3x2=6 3x3=9 Loops Can Reside Inside Other Loops
1x1=1 2x1=2 3x1=3 1x2=2 2x2=4 3x2=6 1x3=3 2x3=6 3x3=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; } 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
1x1=1 2x1=2 3x1=3 1x2=2 2x2=4 3x2=6 1x3=3 2x3=6 3x3=9 Loops Can Reside Inside Other Loops
1x1=1 2x1=2 3x1=3 1x2=2 2x2=4 3x2=6 1x3=3 2x3=6 3x3=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; } 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; } 7 Loops Can Reside Inside Other Loops
1x1=1 2x1=2 3x1=3 1x2=2 2x2=4 3x2=6 1x3=3 2x3=6 3x3=9 Loops Can Reside Inside Other Loops
1x1=1 2x1=2 3x1=3 1x2=2 2x2=4 3x2=6 1x3=3 2x3=6 3x3=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; } 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
1x1=1 2x1=2 3x1=3 2x2=4 3x2=6 Exercise
Which of the programs produces the following output? 1321 2321 3321 3x3=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; } Exercise
Which is the output of number 2? 1 1321 2321 3321 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 Loops with More Complicated Conditionals: Primes
Write a predicate to determine if a number is prime. What would the header look like? 8 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 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 Counters: Primes
What if we want to find the nth prime? nint nthprime(int n) Recall that for the computer True == 1 and False == 0 Use prime(x) Loops with More Complicated Counters: Primes
What if we want to find the nth prime? nint nthprime(int n){ int m=0, p=1; while (m<n){ p=p+1; m = m + prime(p); } return p; } Next Lecture
Data Types 9 ...
View
Full
Document
This note was uploaded on 05/04/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Spring '07 term at University of Michigan.
 Spring '07
 JeffRingenberg
 Volt

Click to edit the document details