Lecture+10 - Engineering 101 Engineering 101 Lecture 10...

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 10 10/4/07 Predicates and Intro to Loops Quote of the Day 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 Some notes on project 3 Some notes on project 3 New format for equations: Some notes on project 3 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 Some notes on project 3 Each calculation of the change in densities must use the old population densities, not the new ones. (.5 * x + y) * ∆t ∆x = ∆y = (12 * x + 3 / y ) * ∆t x = x + ∆x y = y + ∆y newX = x + ∆x newY = y + ∆y x = newX y = newY Correct! 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 Using operators to construct conditional expressions Su M T W Th F Sa 12345 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 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 Using operators to construct conditional expressions int firstday= Tue; int timeclock=0, day = 9; Su M T W Th F Sa 12345 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 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 Using operators to construct conditional expressions How do we determine whether it is a weekday? Su M T W Th F Sa 12345 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 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 Using operators to construct conditional expressions (firstday+day­1) % 7 != Sun When is the above true? Su M T W Th F Sa 12345 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 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 and (firstday+day­1) % 7 != Sat ) { timeclock = timeclock + 8; cout << “I’m working.” << endl; day = day + 1; } Using operators to construct Using operators to construct conditional expressions Su M T W Th F Sa 12345 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 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 and (firstday+day­1) % 7 != Sat ) { timeclock = 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 Using operators to construct conditional expressions Su M T W Th F Sa 12345 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 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 and (firstday+day­1) % 7 != Sat ) { timeclock = 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 Using operators to construct conditional expressions Su M T W Th F Sa 12345 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 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 and (firstday+day­1) % 7 != Sat ) { timeclock = 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 Using operators to construct conditional expressions Su M T W Th F Sa 12345 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 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 and (firstday+day­1) % 7 != Sat ) { timeclock = 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 Using operators to construct conditional expressions Su M T W Th F Sa 12345 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 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 and (firstday+day­1) % 7 != Sat ) { timeclock = timeclock + 8; cout << “I’m working.” << endl; day = day + 1; } How about weekends? Using operators to construct Using operators to construct conditional expressions Su M T W Th F Sa 12345 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 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 and (firstday+day­1) % 7 != Sat ) { Su M T W Th F Sa timeclock = timeclock + 8; 12345 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 27 28 29 30 31 or (firstday+day­1) % 7 == Sat){ cout << “I’m resting.” << endl; day = day + 1; Using operators to construct 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 and (firstday+day­1) % 7 != Sat ) { timeclock = timeclock + 8; cout << “I’m working.” << endl; day = day + 1; } while ((firstday+day­1) % 7 == Sun or (firstday+day­1) % 7 == Sat){ cout << “I’m resting.” << endl; day = day + 1; Using operators to construct Using operators to construct conditional expressions U Y L G O C E D Functions improve clarity 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 and (firstday+day­1) % 7 != Sat ) { timeclock = timeclock + 8; cout << “I’m working.” << endl; day = day + 1; } while ((firstday+day­1) % 7 == Sun or (firstday+day­1) % 7 == Sat){ cout << “I’m resting.” << endl; day = day + 1; Functions improve clarity 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 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 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; } Functions improve clarity Functions improve clarity bool Weekend( int d, int fd){ return ((fd+d­1)%7==0 or (fd+d­1)%7==6); } 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; } Predicates Predicates Functions that return bool (Boolean) values are called predicates. Write some predicates… Which of the predicates returns true if an Which of the predicates returns true if an integer number is divisible by either 2, 3 or 5? Number 3 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 Which of the predicates takes three doubles and returns true if they are sorted highest to lowest or lowest to highest? Number 1 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! (Thanks Aaron) SeaDragon SeaDragon http://labs.live.com/Seadragon.aspx http://www.youtube.com/watch?v=0ra5tp7K­­I# Microsoft 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 Analyzing Loops Analyzing Loops Loops provide the means to reach the solution to a problem by successive approximation. Start ? Goal In some ways a loop is like an inductive proof in mathematics. Factorial Function 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 f = N! Factorial Function 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 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 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 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 Exercise Consider the power function. What is the loop invariant? 1­ z==xy 2­ n==0, z==xn 3­ z==z*x 4­ z==xn Exercise Exercise Consider the power function. What is the loop invariant? 1­ z==xy 2­ n==0, z==xn n is changing, so 3­ z==z*x 4­ z==xn this is always true Exercise Exercise Consider the power function. What is the initialization condition? 1­ n==0 2­ n==0, z==x0 3­ z==x0 4­ n==1, z==x1 Exercise Exercise Consider the power function. What is the initialization condition? 1­ n==0 2­ n==0, z==x0 3­ z==x0 4­ n==1, z==x1 Exercise Exercise Consider the power function. What is the termination condition? 1­ n<y 2­ n>y 3­ n==y 4­ n==n+1 Exercise Exercise Consider the power function. What is the termination condition? 1­ n<y 2­ n>y 3­ n==y 4­ n==n+1 Example: Power x Example: Power x // 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; } y Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x while (n < y){ z z=z*x; n=n+1; } return z; } y power (4,3) y n Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z z=z*x; n=n+1; } return z; } y power (4,3) y3 n Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z1 z=z*x; n=n+1; } return z; } y power (4,3) y3 n0 initialization: n == 0, z == x0 Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z1 z=z*x; n=n+1; } return z; } y power (4,3) y3 n0 Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z4 z=z*x; n=n+1; } return z; } y power (4,3) y3 n1 loop invariant: z= xn Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z4 z=z*x; n=n+1; } return z; } y power (4,3) y3 n1 loop invariant: z= xn Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z 16 z=z*x; n=n+1; } return z; } y power (4,3) y3 n2 loop invariant: z= xn Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z 16 z=z*x; n=n+1; } return z; } y power (4,3) y3 n2 loop invariant: z= xn Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z 64 z=z*x; n=n+1; } return z; } y power (4,3) y3 n3 loop invariant: z= xn Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z 64 z=z*x; n=n+1; } return z; } y power (4,3) y3 n3 termination: n == y Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z 64 z=z*x; n=n+1; } return z; } y 64 y3 n3 Example: Power x Example: Power x // Compute x to the y power double power(double x, int y){ double z=1 int n=0; x4 while (n < y){ z 64 z=z*x; n=n+1; } return z; } y 64 y3 n3 initialization: n == 0, z == x0 loop invariant: z == xn termination: n == y Next Lecture Next Lecture More Loops ...
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