This preview shows page 1. Sign up to view the full content.
Unformatted text preview: Quote of the Day Engineering 101
Lecture 10 10/4/07
Predicates and Intro to Loops It is unwise to be too sure of one's own wisdom. It is healthy to be reminded that the strongest might weaken and the wisest might err.
 Gandhi Some notes on project 3
New format for equations: Some notes on project 3
When calculating the new densities, you must add the old density to the change in the old density.
x = x y = y x = x + x y = y + y
Correct! Some notes on project 3
Each calculation of the change in densities must use the old population densities, not the new ones. x = (.5 * x + y) * t
y = (12 * x + 3 / y ) * t Start on some given day. While the day is not a weekend, add 8 hours to my timeclock and work, otherwise rest. Using operators to construct conditional expressions x = x + x y = y + y newX = x + x newY = y + y x = newX y = newY
Correct! Su M T W Th F Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6;
firstday tells which day of the week the first day of the month falls upon Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, Su M T W Th F Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
How do we determine whether it is a weekday? Su M T W Th F Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, This is only true on weekdays while ( (firstday+day1) % 7 != Sun (firstday+dayand (firstday+day1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; } (firstday+day1) % 7 != Sun (firstday+dayWhen is the above true? Su M T W Th F Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Su M T W Th F Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, while ( (firstday+day1) % 7 != Sun (firstday+dayand (firstday+day1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; }
What is the initialization? What is the condition? Where is the loop incremented? Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, 9; while ( (firstday+day1) % 7 != Sun (firstday+dayand (firstday+day1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; }
What is the initialization? What is the condition? Where is the loop incremented? Su M T W Th F Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Su M T W Th F Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 2 Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, 9; while ( (firstday+day1) % 7 != Sun (firstday+dayand (firstday+day1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; }
What is the initialization? What is the condition? Where is the loop incremented? Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, 9; while ( (firstday+day1) % 7 != Sun (firstday+dayand (firstday+day1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; }
What is the initialization? What is the condition? Where is the loop incremented? Su M T W Th F Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Su M T W Th F Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, while ( (firstday+day1) % 7 != Sun (firstday+dayand (firstday+day1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; }
How about weekends? Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, while ( (firstday+day1) % 7 != Sun (firstday+dayand (firstday+day1) % 7 != Sat ) { (firstday+daySu M T W Th F Sa timeclock = timeclock + 8; 1 2 3 4 5 cout << "I'm working." << endl; working." endl; 6 7 8 9 10 11 12 day = day + 1; 13 14 15 16 17 18 19 } 20 21 22 23 24 25 26 while ((firstday+day1) % 7 == Sun ((firstday+day27 28 29 30 31 or (firstday+day1) % 7 == Sat){ (firstday+daySat){ cout << "I'm resting." << endl; resting." endl; day = day + 1; } Su M T W Th F Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 Using operators to construct conditional expressions
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, while ( (firstday+day1) % 7 != Sun (firstday+dayand (firstday+day1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; } while ((firstday+day1) % 7 == Sun ((firstday+dayor (firstday+day1) % 7 == Sat){ (firstday+daycout << "I'm resting." << endl; resting." endl; day = day + 1; } Functions improve clarity
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, while ( (firstday+day1) % 7 != Sun (firstday+dayand (firstday+day1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; } while ((firstday+day1) % 7 == Sun ((firstday+dayor (firstday+day1) % 7 == Sat){ (firstday+daycout << "I'm resting." << endl; resting." endl; day = day + 1; } U LY G C DE O 3 Functions improve clarity
int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, while ( not Weekend(day, firstday) ) { firstday) timeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; } while ( Weekend(day, firstday) ){ firstday) cout << "I'm resting." << endl; resting." endl; day = day + 1; } Functions improve clarity
int main ( ){ int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, while ( not Weekend(day, firstday) ) { firstday) timeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; } while ( Weekend(day, firstday) ){ firstday) cout << "I'm resting." << endl; resting." endl; day = day + 1; } Functions improve clarity
bool Weekend( int d, int fd) fd) int main ( ){ int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, while ( not Weekend(day, firstday) ) { firstday) timeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; } while ( Weekend(day, firstday) ){ firstday) cout << "I'm resting." << endl; endl; resting." day = day + 1; } Functions improve clarity
bool Weekend( int d, int fd){ fd){ return ((fd+d1)%7==0 or (fd+d1)%7==6); } ((fd+d(fd+dint main ( ){ int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; firstday= int timeclock=0, day = 9; timeclock=0, while ( not Weekend(day, firstday) ) { firstday) timeclock = timeclock + 8; cout << "I'm working." << endl; working." endl; day = day + 1; } while ( Weekend(day, firstday) ){ firstday) cout << "I'm resting." << endl; resting." endl; day = day + 1; } Predicates
Functions that return bool (Boolean) values are called predicates. Write some predicates... predicates... Which of the predicates returns true if an integer number is divisible by either 2, 3 or 5? 4 Number 3
3. bool twothreefive (int x){ return ( (x % 2 == 0) or (x % 3 == 0) or (x % 5 == 0) ); } Which of the predicates takes three doubles and returns true if they are sorted highest to lowest or lowest to highest? Number 1
1. bool sorted (double x, double y, double z){ return ( (x >= y and y >= z) or (x <= y and y <= z) ); } News Flash!
SeaDragon
http://labs.live.com/Seadragon.aspx http://labs.live.com/Seadragon.aspx http://www.youtube.com/watch?v=0ra5tp7KI# http://www.youtube.com/watch?v=0ra5tp7KI# Microsoft Photosynth
http://labs.live.com/photosynth/ http://labs.live.com/photosynth/ http://www.youtube.com/watch?v=p16frKJLVi0 Microsoft Surface
http://www.microsoft.com/surface/ http://www.youtube.com/watch?v=rP5y7yp06n0 http://www.youtube.com/watch?v=CZrr7AZ9nCY http://www.youtube.com/watch?v=CZrr7AZ9nCY Analyzing Loops
Loops provide the means to reach the solution to a problem by successive approximation.
Start Factorial Function
int fact( int N){ int count=1, f=1; while (count < N){ f = f * (count + 1); count = count + 1; } return f; } Start ? Goal In some ways a loop is like an inductive proof in mathematics. Goal f = N! 5 Factorial Function
int fact( int N){ int count=1, f=1; while (count < N){ f = f * (count + 1); count = count + 1; } return f; } Start The loop body moves the data toward the goal Goal f = N! Factorial Function
int fact( int N){ int count=1, f=1; while (count < N){ f = f * (count + 1); count = count + 1; } return f; } Start The loop invariant is preserved after each iteration Goal f = N! Factorial Function
int fact( int N){ Start int count=1, f=1; while (count < N){ invariant f = f * (count + 1); f = count! count = count + 1; } return f; After each iteration count has advanced } Goal by 1 and the invariant f = N! is preserved. Factorial Function
int fact( int N){ Start f = 1! int count=1, f=1; while (count < N){ invariant f = f * (count + 1); f = count! count = count + 1; } return f; The initialization condition } establishes the truth of Goal the invariant before f = N! the loop commences Exercise
Consider the power function. What is the loop invariant? invariant? 1 z==xy 2 n==0, z==xn z==x z==x 3 z==z*x 4 z==xn x z== Exercise
Consider the power function. What is the loop invariant? invariant? 1 z==xy x 2 n==0, z==xn z== z==x n is changing, so n 3 z==z*x 4 z==x this is always true z==x 6 Exercise
Consider the power function. What is the initialization condition? condition? 1 n==0 2 n==0, z==x0 3 z==x0 4 n==1, z==x1 Exercise
Consider the power function. What is the initialization condition? condition? 1 n==0 2 n==0, z==x0 3 z==x0 4 n==1, z==x1 Exercise
Consider the power function. What is the termination condition? condition? 1 n<y 2 n>y 3 n==y 4 n==n+1 Exercise
Consider the power function. What is the termination condition? condition? 1 n<y 2 n>y 3 n==y 4 n==n+1 Example: Power xy
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; while (n < y){ z=z*x; n=n+1; } return z; } Example: Power xy
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; power (4,3) y n x z while (n < y){ z=z*x; n=n+1; } return z; } 7 Example: Power xy
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; Example: Power xy
power (4,3)
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; power (4,3) y 3 n 0 initialization: n == 0, z == x0 x 4 z y 3 n x 4 z 1 while (n < y){ z=z*x; n=n+1; } return z; } while (n < y){ z=z*x; n=n+1; } return z; } Example: Power xy
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; Example: Power xy
power (4,3)
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; power (4,3) y 3 n 1 loop invariant: z= xn x 4 z 1 y 3 n 0 x 4 z 4 while (n < y){ z=z*x; n=n+1; } return z; } while (n < y){ z=z*x; n=n+1; } return z; } Example: Power xy
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; Example: Power xy
power (4,3)
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; power (4,3) y 3 n 2 loop invariant: z= xn x 4 z 4 y 3 n 1 loop invariant: z= xn x 4 while (n < y){ z=z*x; n=n+1; } return z; } while (n < y){ z=z*x; n=n+1; } return z; } z 16 8 Example: Power xy
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; Example: Power xy
power (4,3)
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; power (4,3) y 3 n 3 loop invariant: z= xn x 4 y 3 n 2 loop invariant: z= xn x 4 while (n < y){ z=z*x; n=n+1; } return z; } z 16 while (n < y){ z=z*x; n=n+1; } return z; } z 64 Example: Power xy
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; Example: Power xy
power (4,3)
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; 64 y 3 n 3 x 4 y 3 n 3 x 4 while (n < y){ z=z*x; n=n+1; } return z; } z 64 termination: n == y while (n < y){ z=z*x; n=n+1; } return z; } z 64 Example: Power xy
// Compute x to the y power double power(double x, int y){ double z=1 int n=0; Next Lecture
More Loops
64 x 4 y 3 n 3 initialization: n == 0, z == x0 loop invariant: z == xn termination: n == y while (n < y){ z=z*x; n=n+1; } return z; } z 64 9 ...
View
Full
Document
 Fall '07
 Ringenberg

Click to edit the document details