{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

07 - Predicates and Intro to Loops

# 07 - Predicates and Intro to Loops - Engineering 101 Quote...

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

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day 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 Predicates and Intro to Loops 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 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 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 int firstday= Tue; int timeclock=0, day = 9; 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; int timeclock=0, day = 9; Using operators to construct conditional expressions int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; int timeclock=0, day = 9; Su M T W Th F Sa How do we determine whether it is a weekday? 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 (firstday+day(firstday+day-1) % 7 != Sun When 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 1 Using operators to construct conditional expressions int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; int timeclock=0, day = 9; This is only true on weekdays while ( (firstday+day-1) % 7 != Sun (firstday+dayand (firstday+day-1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; day = day + 1; } Using operators to construct conditional expressions int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; int timeclock=0, day = 9; while ( (firstday+day-1) % 7 != Sun (firstday+dayand (firstday+day-1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm 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; int timeclock=0, day = 9; 9; while ( (firstday+day-1) % 7 != Sun (firstday+dayand (firstday+day-1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm 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; int timeclock=0, day = 9; 9; while ( (firstday+day-1) % 7 != Sun (firstday+dayand (firstday+day-1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm 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; int timeclock=0, day = 9; 9; while ( (firstday+day-1) % 7 != Sun (firstday+dayand (firstday+day-1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm 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; int timeclock=0, day = 9; while ( (firstday+day-1) % 7 != Sun (firstday+dayand (firstday+day-1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; day = day + 1; } How about weekends? 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; int timeclock=0, day = 9; while ( (firstday+day-1) % 7 != Sun (firstday+dayand (firstday+day-1) % 7 != Sat ) { (firstday+daySu M T W Th F Sa timeclock = timeclock + 8; 1 2 3 4 5 cout << "I'm 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+day-1) % 7 == Sun ((firstday+day27 28 29 30 31 or (firstday+day-1) % 7 == Sat){ (firstday+daySat){ cout << "I'm resting." << endl; day = day + 1; } Using operators to construct conditional expressions int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; int timeclock=0, day = 9; while ( (firstday+day-1) % 7 != Sun (firstday+dayand (firstday+day-1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; day = day + 1; } while ((firstday+day-1) % 7 == Sun ((firstday+dayor (firstday+day-1) % 7 == Sat){ (firstday+daycout << "I'm 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; int timeclock=0, day = 9; while ( (firstday+day-1) % 7 != Sun (firstday+dayand (firstday+day-1) % 7 != Sat ) { (firstday+daytimeclock = timeclock + 8; cout << "I'm working." << endl; day = day + 1; } while ((firstday+day-1) % 7 == Sun ((firstday+dayor (firstday+day-1) % 7 == Sat){ (firstday+daycout << "I'm 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; int timeclock=0, day = 9; while ( not Weekend(day, firstday) ) { timeclock = timeclock + 8; cout << "I'm working." << endl; day = day + 1; } while ( Weekend(day, firstday) ){ cout << "I'm 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; int timeclock=0, day = 9; while ( not Weekend(day, firstday) ) { timeclock = timeclock + 8; cout << "I'm working." << endl; day = day + 1; } while ( Weekend(day, firstday) ){ cout << "I'm resting." << endl; day = day + 1; } Functions improve clarity bool Weekend( int d, int fd) int main ( ){ int Sun=0, Mon=1, Tue=2, Wed=3, Thu=4, Fri=5, Sat=6; int firstday= Tue; int timeclock=0, day = 9; while ( not Weekend(day, firstday) ) { timeclock = timeclock + 8; cout << "I'm working." << endl; day = day + 1; } while ( Weekend(day, firstday) ){ cout << "I'm resting." << endl; day = day + 1; } 3 Functions improve clarity bool Weekend( int d, int fd){ return ((fd+d-1)%7==0 or (fd+d-1)%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; int timeclock=0, day = 9; while ( not Weekend(day, firstday) ) { timeclock = timeclock + 8; cout << "I'm working." << endl; day = day + 1; } while ( Weekend(day, firstday) ){ cout << "I'm resting." << endl; day = day + 1; } Predicates Functions that return bool (Boolean) values are called predicates. Write some predicates... Which of the predicates returns true if an integer number is divisible by either 2, 3 or 5? Which of the predicates takes three doubles and returns true if they are sorted highest to lowest or lowest to highest? 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! 4 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 3- z==z*x 4- z==xn Exercise Consider the power function. What is the initialization condition? condition? 1- n==0 2- n==0, z==x0 0 3- z==x 4- n==1, z==x1 5 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; 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 x while (n < y){ z=z*x; n=n+1; } return z; } y n while (n < y){ z=z*x; n=n+1; } return z; } x 4 z 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 0 x 4 while (n < y){ z=z*x; n=n+1; } return z; } y 3 n 0 initialization: n == 0, z == x0 x 4 while (n < y){ z=z*x; n=n+1; } return z; } z 1 z 1 6 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 while (n < y){ z=z*x; n=n+1; } return z; } y 3 n 1 loop invariant: z= xn while (n < y){ z=z*x; n=n+1; } return z; } x 4 z 4 z 4 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 while (n < y){ z=z*x; n=n+1; } return z; } y 3 n 2 loop invariant: z= xn while (n < y){ z=z*x; n=n+1; } return z; } x 4 z 16 z 16 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 x 4 while (n < y){ z=z*x; n=n+1; } return z; } y 3 n 3 loop invariant: z= xn while (n < y){ z=z*x; n=n+1; } return z; } x 4 z 64 z 64 termination: n == y 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 64 // Compute x to the y power double power(double x, int y){ double z=1 int n=0; 64 y 3 n 3 initialization: n == 0, z == x0 loop invariant: z == xn termination: n == y x 4 while (n < y){ z=z*x; n=n+1; } return z; } y 3 n 3 while (n < y){ z=z*x; n=n+1; } return z; } x 4 z 64 z 64 Next Lecture More Loops 8 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online