Download presentation
Presentation is loading. Please wait.
Published byClaud Sanders Modified over 9 years ago
1
Intro to for loops Common sequence of code: x=0; while (x<12) { printf("%d ",x); x++; } Output: 0 1 2 3 4 5 6 7 8 9 10 11
2
Intro to for loops Common sequence of code: x=0; while (x<12) { printf("%d ",x); x++; } Equivalent for construct 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
3
Predict output of: for (i=5; i<40; i+=8) { printf("%d ", i); } OUTPUT:__5 13 21 29 37___ for (i=-5; i<-10; i--) { printf("%d ", i); } OUTPUT: No output for (i=10; i<=100; i=i+10) { if (i%20) printf("%d ", i); } OUTPUT:_ 10 30 50 70 90 for (i=5; i<10; i+=i%2) { printf("%d ", i++); } OUTPUT:__
4
Nested for loops: int i,j; : for (i=1; i<=3; i++) { for (j=1; j<=4; j++) { printf("%d %d\n",i,j); } } Output: 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 3 1 3 2 3 3 3 4
5
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.
6
Cows, Pigs, Hens: Unknowns & equations: Unknowns nC - Number Cows nP - Number Pigs nH - Number Hens Equations: nC + nP + nH = 1000 nC * 5 + nP * 3 + nH * 0.5 = 1000 The problem: 3 unknowns, 2 equations - there is no UNIQUE sol'n However, there may be several solutions.
7
Cows, Pigs, Hens: Loop code for (nC = 0; nC <= 1000; nC++) { for (nP = 0; nP <= 1000; nP++) { for (nH = 0; nH <= 1000; nH++) { // // tests here // } } }
8
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 nC;// number cows int nP;// number pigs int nH;// number hens
9
Cows, Pigs, Hens: "Boiler plate" #include void main(void) { }
10
Cows, Pigs, Hens: The tests if ( nC * COW + nP * PIG + nH * HEN == 1000.0 ) { if ( nC + nP + nH == 1000 ) { printf("Buy%4d Cows,%4d Pigs, and%4d Hens\n", nC,nP,nH); } }
11
Cows, Pigs, Hens: The code (version 1) #include void main(void) { int nC, nP, nH; for (nC = 0; nC <= 1000; nC++) { for (nP = 0; nP <= 1000; nP++) { for (nH = 0; nH <= 1000; nH++) { if ( nC * COW + nP * PIG + nH * HEN == 1000.0 ) { if ( nC + nP + nH == 1000 ) { printf("%4d Cows,%4d Pigs,%4d Hens\n",nC,nP,nH); } } } } } } #define COW 5.00 #define PIG 3.00 #define HEN 0.50
12
Cows, Pigs, Hens: # times each line executed #include 0 void main(void) 0 { int nC, nP, nH; 0 for (nC = 0; nC <= 1000; nC++) { for (nP = 0; nP <= 1000; nP++) { for (nH = 0; nH <= 1000; nH++) { if ( nC * COW + nP * PIG + nH * HEN == 1000.0 ) { if ( nC + nP + nH == 1000 ) { printf("%4d Cows,%4d Pigs,%4d Hens\n",nC,nP,nH); } } } } } } #define COW 5.00 0 #define PIG 3.00 0 #define HEN 0.50 0
13
Cows, Pigs, Hens: # times each line executed 1 1000 1000 for (nC = 0; nC <= 1000; nC++) { 1000 1000000 1000000 for (nP = 0; nP <= 1000; nP++) { 1000000 1000000000 1000000000 for (nH = 0; nH <= 1000; nH++) { 1000000000 if ( nC * COW + nP * PIG + nH * HEN == 1000.0 ) { 1000000 if ( nC + nP + nH == 1000 ) (say 0.1%) { printf("%4d Cows,%4d Pigs,%4d Hens\n",nC,nP,nH); } 1000 } (say 0.1%) } } }
14
Cows, Pigs, Hens: Timing Instruction#times#cycTime(uS) nC=0110.001 nC<=1000100022.000 nC++100011.000 nP=0100011.000 nP<=1000100000022000.000 nP++100000011000.000 nH=0100000011000.000 nH<=1000100000000022000000.000 nH++100000000011000000.000 if(nc*COW+nP...100000000077000000.000 if(nC+nP+nH...100000044000.000 printf1000 1000000.000 Total11008004.001
15
Cows, Pigs, Hens: Efficiancy considerations Old: for (nC=0; nC<=1000; nC++) But:The max nC could be is 1000/5=200 New: for (nC=0; nC<=200; nC++) Old: for (nP=0; nP<=1000; nP++) But:The max nP could be is 1000/3 = 333 New: for (nP=0; nP<=333; nP++) Old: for (nH=0; nH<=1000; nH++) But: You can never buy an odd number of hens New: for (nH=0; nH<=1000; nH+=2) Old:order of if statements But: if(nC*COW+nP*PIG... takes two times longer than if(nC+nP+nH... New:swap order of if statements
16
Cows, Pigs, Hens: The code (version 2) #include void main(void) { int nC, nP, nH; for (nC = 0; nC <= 200; nC++) { for (nP = 0; nP <= 333; nP++) { for (nH = 0; nH <= 1000; nH+=2) { if ( nC + nP + nH == 1000 ) { if ( nC * COW + nP * PIG + nH * HEN == 1000.0 ) { printf("%4d Cows,%4d Pigs,%4d Hens\n",nC,nP,nH); } } } } } } #define COW 5.00 #define PIG 3.00 #define HEN 0.50
17
Cows, Pigs, Hens: Timing (Version 2) Instruction#times#cycTime(uS) nC=0110.001 nC<=100020020.200 nC++20010.200 nP=020010.200 nP<=1000666002133.200 nP++66600166.600 nH=066600166.600 nH<=100033300000266600.000 nH++33300000133300.000 if(nc+nP+nH...333000004133200.000 if(Nc*COW+nP...3330723.310 printf310003000.000 Total236390.311
18
Cows, Pigs, Hens: Other considerations Old:Suppose the problem has no solution But:In that case there will be no output New:Create a SolutionFound flag Initialize: solutionFound=0; After the print statement add: solutionFound=1; After last } of for (nC=0... loop add: if (!solutionFound) printf("No solution\n"); Old:Several sets of { } that are not needed New:Remove extra { }
19
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 nC, nP, nH; solutionFound=0; for (nC = 0; nC <= 200; nC++) for (nP = 0; nP <= 333; nP++) for (nH = 0; nH <= 1000; nH+=2) if ( nC + nP + nH == 1000 ) if ( nC * COW + nP * PIG + nH * HEN == 1000.0 ) { printf("%4d Cows,%4d Pigs,%4d Hens\n",nC,nP,nH); solutionFound=1; } if (!soluitonFound) printf("No Solution\n"); }
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.