guyy
YaBB Newbies
Offline
Posts: 13
Re: Processing project... coin flip
Apr 24th , 2010, 9:52pm
Hi, There were a few errors in your code. first of all you were using the same variable (numHeads) for the for loop and for counting the results. This caused numHeads to be reused inside the loop and miscount. Note that I reset numHeads to 0 at the beginning of each trial. The second thing is that in order to increment a variable you should either use x = x + 1 or x++ but not both (i.e. x = x++); You should also read a little about arrays as it will make your life easier. anyway here's the working code: int zeroOfTen; int oneOfTen ; int twoOfTen; int threeOfTen; int four0fTen; int fiveOfTen; int sixOfTen; int sevenOfTen; int eightOfTen; int nineOfTen; int tenOfTen; int numHeads; int trialNumber; int flipNumber; void setup () { size (400,400); background (10,100,25); simulate ( ) ; println ( "Done simulating" ) ; drawGraph ( ) ; noLoop ( ) ; } void simulate ( ) { for(trialNumber=0;trialNumber<1000;trialNumber++) { numHeads = 0; for (int i=0;i<10;i++) { Boolean isHeads = false; isHeads = (random (0,1) > 0.5); if (isHeads) numHeads++; } switch(numHeads){ case 0: zeroOfTen++; break; case 1: oneOfTen++; break; case 2: twoOfTen++; break; case 3: threeOfTen ++; break; case 4: four0fTen ++; break; case 5: fiveOfTen ++; break; case 6: sixOfTen ++; break; case 7: sevenOfTen ++; break; case 8: eightOfTen ++; break; case 9: nineOfTen ++; break; case 10: tenOfTen ++; break; } } } void drawGraph ( ) { for(flipNumber=1;flipNumber<10;flipNumber++) { fill(0,0,50); rect(0,50,zeroOfTen,20); rect(0,80,oneOfTen,20); rect(0,110,twoOfTen,20); rect(0,140,threeOfTen,20); rect(0,170,four0fTen,20); rect(0,200,fiveOfTen,20); rect(0,230,sixOfTen,20); rect(0,260,sevenOfTen,20); rect(0,290,eightOfTen,20); rect(0,320,nineOfTen,20); rect(0,350,tenOfTen,20); } }