Upload
claud-sanders
View
212
Download
0
Embed Size (px)
Citation preview
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
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
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:__
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 11 21 31 42 12 22 32 43 13 23 33 4
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:
UnknownsnC - Number CowsnP - Number PigsnH - 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'nHowever, there may be several solutions.
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 // } } }
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 cowsint nP; // number pigsint nH; // number hens
Cows, Pigs, Hens: "Boiler plate"#include <stdio.h>void main(void){
}
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); }}
Cows, Pigs, Hens: The code (version 1)#include <stdio.h>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
Cows, Pigs, Hens: # times each line executed#include <stdio.h> 0void 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
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%) } }}
Cows, Pigs, Hens: TimingInstruction #times #cyc Time(uS)
nC=0 1 1 0.001
nC<=1000 1000 2 2.000
nC++ 1000 1 1.000
nP=0 1000 1 1.000
nP<=1000 1000000 2 2000.000
nP++ 1000000 1 1000.000
nH=0 1000000 1 1000.000
nH<=1000 1000000000 2 2000000.000
nH++ 1000000000 1 1000000.000
if(nc*COW+nP... 1000000000 7 7000000.000
if(nC+nP+nH... 1000000 4 4000.000
printf 1000 1000 1000000.000
Total 11008004.001
Cows, Pigs, Hens: Efficiancy considerationsOld: for (nC=0; nC<=1000; nC++) But: The max nC could be is 1000/5=200New: for (nC=0; nC<=200; nC++)
Old: for (nP=0; nP<=1000; nP++)But: The max nP could be is 1000/3 = 333New: for (nP=0; nP<=333; nP++)
Old: for (nH=0; nH<=1000; nH++)But: You can never buy an odd number of hensNew: for (nH=0; nH<=1000; nH+=2)
Old: order of if statementsBut: if(nC*COW+nP*PIG ...
takes two times longer than if(nC+nP+nH...
New: swap order of if statements
Cows, Pigs, Hens: The code (version 2)#include <stdio.h>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
Cows, Pigs, Hens: Timing (Version 2)Instruction #times #cyc Time(uS)
nC=0 1 1 0.001
nC<=1000 200 2 0.200
nC++ 200 1 0.200
nP=0 200 1 0.200
nP<=1000 66600 2 133.200
nP++ 66600 1 66.600
nH=0 66600 1 66.600
nH<=1000 33300000 2 66600.000
nH++ 33300000 1 33300.000
if(nc+nP+nH... 33300000 4 133200.000
if(Nc*COW+nP... 3330 7 23.310
printf 3 1000 3000.000
Total 236390.311
Cows, Pigs, Hens: Other considerationsOld: Suppose the problem has no solution But: In that case there will be no outputNew: 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 neededNew: Remove extra { }
Cows, Pigs, Hens: The code (version 3/Final)#include <stdio.h>#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");}