Скачать презентацию Intro to for loops Common sequence of code Скачать презентацию Intro to for loops Common sequence of code

c4879f4be0586784b41d00558ac2d953.ppt

  • Количество слайдов: 19

Intro to for loops Common sequence of code: Output: x=0; while (x<12) { printf( Intro to for loops Common sequence of code: Output: x=0; while (x<12) { printf("%d ", x); x++; } 0 1 2 3 4 5 6 7 8 9 10 11

Intro to for loops Common sequence of code: Equivalent for construct x=0; while (x<12) Intro to for loops Common sequence of code: Equivalent for construct x=0; while (x<12) { printf("%d ", x); x++; } for (x=0 ; x<12 ; x++ ) { printf("%d ", x); } Initial value Test condition Body of loop (may be 0, 1, or several statements) End of loop change

Predict output of: for (i=5; i<40; i+=8) { printf( Predict output of: for (i=5; i<40; i+=8) { printf("%d ", i); } for (i=10; i<=100; i=i+10) { if (i%20) printf("%d ", i); } OUTPUT: __5 13 21 29 37___ OUTPUT: _ 10 30 50 70 90 for (i=-5; i<-10; i--) { printf("%d ", i); } for (i=5; i<10; i+=i%2) { printf("%d ", i++); } OUTPUT: No output OUTPUT: __

Nested for loops: int i, j; : for (i=1; i<=3; i++) { for (j=1; Nested for loops: int i, j; : for (i=1; i<=3; i++) { for (j=1; j<=4; j++) { printf("%d %dn", i, j); } } Output: 1 1 2 2 3 3 1 2 3 4

Cows, Pigs, Hens: Problem statement: A farmer goes to market. The farmer wants to Cows, Pigs, Hens: Problem statement: A farmer goes to market. The farmer wants to buy exactly 1000 animals, and spend exactly $1000. Cows cost $5; Pigs cost $3; Hens cost $0. 50 What number and type of each animal should the farmer buy? Write a program to determine all possible combinations (if any). Then test your program for efficiency.

Cows, Pigs, Hens: Unknowns & equations: Unknowns n. C - Number Cows n. P Cows, Pigs, Hens: Unknowns & equations: Unknowns n. C - Number Cows n. P - Number Pigs n. H - Number Hens Equations: n. C + n. P + n. H = 1000 n. C * 5 + n. P * 3 + n. H * 0. 5 = 1000 The problem: 3 unknowns, 2 equations - there is no UNIQUE sol'n However, there may be several solutions.

Cows, Pigs, Hens: Loop code for (n. C = 0; n. C <= 1000; Cows, Pigs, Hens: Loop code for (n. C = 0; n. C <= 1000; n. C++) { for (n. P = 0; n. P <= 1000; n. P++) { for (n. H = 0; n. H <= 1000; n. H++) { // // tests here // } } }

Cows, Pigs, Hens: Define some constants & variables // define cost for each animal Cows, Pigs, Hens: Define some constants & variables // define cost for each animal #define COW 5. 00 #define PIG 3. 00 #define HEN 0. 50 int n. C; int n. P; int n. H; // number cows // number pigs // number hens

void main(void) { } " src="https://present5.com/presentation/c4879f4be0586784b41d00558ac2d953/image-9.jpg" alt="Cows, Pigs, Hens: "Boiler plate" #include void main(void) { } " /> Cows, Pigs, Hens: "Boiler plate" #include void main(void) { }

Cows, Pigs, Hens: The tests if ( n. C * COW + n. P Cows, Pigs, Hens: The tests if ( n. C * COW + n. P * PIG + n. H * HEN == 1000. 0 ) { if ( n. C + n. P + n. H == 1000 ) { printf("Buy%4 d Cows, %4 d Pigs, and%4 d Hensn", n. C, n. P, n. H); } }

Cows, Pigs, Hens: The code (version 1) #include <stdio. h> #define COW 5. 00 Cows, Pigs, Hens: The code (version 1) #include #define COW 5. 00 void main(void) #define PIG 3. 00 { #define HEN 0. 50 int n. C, n. P, n. H; for (n. C = 0; n. C <= 1000; n. C++) { for (n. P = 0; n. P <= 1000; n. P++) { for (n. H = 0; n. H <= 1000; n. H++) { if ( n. C * COW + n. P * PIG + n. H * HEN == 1000. 0 ) { if ( n. C + n. P + n. H == 1000 ) { printf("%4 d Cows, %4 d Pigs, %4 d Hensn", n. C, n. P, n. H); } } }

Cows, Pigs, Hens: # times each line executed #include <stdio. h> void main(void) { Cows, Pigs, Hens: # times each line executed #include void main(void) { int n. C, n. P, n. H; 0 0 0 #define COW 5. 00 #define PIG 3. 00 #define HEN 0. 50 for (n. C = 0; n. C <= 1000; n. C++) { for (n. P = 0; n. P <= 1000; n. P++) { for (n. H = 0; n. H <= 1000; n. H++) { if ( n. C * COW + n. P * PIG + n. H * HEN == 1000. 0 ) { if ( n. C + n. P + n. H == 1000 ) { printf("%4 d Cows, %4 d Pigs, %4 d Hensn", n. C, n. P, n. H); } } } 0 0 0

Cows, Pigs, Hens: # times each line executed 1 1000 for (n. C = Cows, Pigs, Hens: # times each line executed 1 1000 for (n. C = 0; n. C <= 1000; n. C++) { for (n. P = 0; n. P <= 1000; n. P++) { 1000000 1000000000 for (n. H = 0; n. H <= 1000; n. H++) { 100000 if ( n. C * COW + n. P * PIG + n. H * HEN == 1000. 0 ) { 1000000 if ( n. C + n. P + n. H == 1000 ) (say 0. 1%) { printf("%4 d Cows, %4 d Pigs, %4 d Hensn", n. C, n. P, n. H); } 1000 } (say 0. 1%) } }

Cows, Pigs, Hens: Timing Instruction #times #cyc Time(u. S) n. C=0 n. C<=1000 n. Cows, Pigs, Hens: Timing Instruction #times #cyc Time(u. S) n. C=0 n. C<=1000 n. C++ 1 1000 1 2 1 0. 001 2. 000 1. 000 n. P=0 n. P<=1000 n. P++ 1000000 1 2 1 1. 000 2000. 000 1000000 1 100000 2 100000 1 100000 7 1000000 4 1000. 000 2000000. 000 1000000. 000 7000000. 000 4000. 000 1000000. 000 n. H=0 n. H<=1000 n. H++ if(nc*COW+n. P. . . if(n. C+n. P+n. H. . . printf Total 11008004. 001

Cows, Pigs, Hens: Efficiancy considerations Old: for (n. C=0; n. C<=1000; n. C++) But: Cows, Pigs, Hens: Efficiancy considerations Old: for (n. C=0; n. C<=1000; n. C++) But: The max n. C could be is 1000/5=200 New: for (n. C=0; n. C<=200; n. C++) Old: for (n. P=0; n. P<=1000; n. P++) But: The max n. P could be is 1000/3 = 333 New: for (n. P=0; n. P<=333; n. P++) Old: for (n. H=0; n. H<=1000; n. H++) But: You can never buy an odd number of hens New: for (n. H=0; n. H<=1000; n. H+=2) Old: But: order of if statements if(n. C*COW+n. P*PIG. . . takes two times longer than if(n. C+n. P+n. H. . . New: swap order of if statements

Cows, Pigs, Hens: The code (version 2) #include <stdio. h> #define COW 5. 00 Cows, Pigs, Hens: The code (version 2) #include #define COW 5. 00 void main(void) #define PIG 3. 00 { #define HEN 0. 50 int n. C, n. P, n. H; for (n. C = 0; n. C <= 200; n. C++) { for (n. P = 0; n. P <= 333; n. P++) { for (n. H = 0; n. H <= 1000; n. H+=2) { if ( n. C + n. P + n. H == 1000 ) { if ( n. C * COW + n. P * PIG + n. H * HEN == 1000. 0 ) { printf("%4 d Cows, %4 d Pigs, %4 d Hensn", n. C, n. P, n. H); } } }

Cows, Pigs, Hens: Timing (Version 2) Instruction #times #cyc Time(u. S) n. C=0 n. Cows, Pigs, Hens: Timing (Version 2) Instruction #times #cyc Time(u. S) n. C=0 n. C<=1000 n. C++ 1 200 1 2 1 0. 001 0. 200 n. P=0 n. P<=1000 n. P++ 200 66600 1 2 1 0. 200 133. 200 66. 600 66600 1 33300000 2 33300000 1 33300000 4 3330 7 3 1000 66. 600 66600. 000 33300. 000 133200. 000 23. 310 3000. 000 n. H=0 n. H<=1000 n. H++ if(nc+n. P+n. H. . . if(Nc*COW+n. P. . . printf Total 236390. 311

Cows, Pigs, Hens: Other considerations Old: Suppose the problem has no solution But: In Cows, Pigs, Hens: Other considerations Old: Suppose the problem has no solution But: In that case there will be no output New: Create a Solution. Found flag Initialize: solution. Found=0; After the print statement add: solution. Found=1; After last } of for (n. C=0. . . loop add: if (!solution. Found) printf("No solutionn"); Old: Several sets of { } that are not needed New: Remove extra { }

Cows, Pigs, Hens: The code (version 3/Final) #include <stdio. h> #define COW 5. 00 Cows, Pigs, Hens: The code (version 3/Final) #include #define COW 5. 00 #define PIG 3. 00 #define HEN 0. 50 void main(void) { int n. C, n. P, n. H; solution. Found=0; for (n. C = 0; n. C <= 200; n. C++) for (n. P = 0; n. P <= 333; n. P++) for (n. H = 0; n. H <= 1000; n. H+=2) if ( n. C + n. P + n. H == 1000 ) if ( n. C * COW + n. P * PIG + n. H * HEN == 1000. 0 ) { printf("%4 d Cows, %4 d Pigs, %4 d Hensn", n. C, n. P, n. H); solution. Found=1; } if (!soluiton. Found) printf("No Solutionn"); }