Write a C program that will \"drop balls\" each of which will bounce randomly an
ID: 3623392 • Letter: W
Question
Write a C program that will "drop balls" each of which will bounce randomly and land in a slot. There will be 21 slots (numbered from -10 to 10). Each ball will be dropped over the middle slot (number 0) and "bounce" 10 times. On each bounce the ball will move (randomly) either left or right (i.e. to the slot with one number lower or one higher). Notice that since there will be an even number of bounces the ball will always land in an even-numbered slot.The program will ask the user for the number of balls to be dropped and accept the user's answer. If the user enters a value less than 1 an error message will be displayed and the program will continue asking until a value at least 1 is entered. The program will then drop the requested number of balls and count how many land in each slot. A histogram will be displayed showing how many balls landed in each slot (you need only draw histogram lines for even-numbered slots). Use lower case 'o' for points in the histogram -- each 'o' will represent one ball. The histogram should (roughly) present a bell curve representing a normal distribution. Running the program may give a display as follows: Your program must be well-commented (of course), show good programming style, use several functions, and have a short main(). Each function other than main() should have a function prototype.
You need to use an int array, say int catch[21], to count the number of balls which fall into the various slots: catch[10] will hold the number of balls which fall into the middle slot, catch[11] the number in the slot to its right, catch[9] the slot one left of the middle, etc. To "drop" a ball you will initialize a counter to 10 (for the middle slot) and let it "bounce" 10 times. A bounce will randomly add or subtract 1 from this counter. After the 10 bounces the value in the counter will be the slot where the ball lands -- increment the corresponding position in catch[].
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <conio.h>
#include <ctime>
#include <stdlib.h>
int getnumber(void);
void getslot(int []);
void print(int []);
int main()
{int balls=3,slotcount[21]={0},slot;
int i,j;
srand((unsigned)time(NULL));
balls=getnumber();
for(i=0;i<balls;i++)
getslot(slotcount);
print(slotcount);
getch();
return 0;
}
int getnumber(void)
{int balls;
do{
printf("How many balls to drop? ");
scanf("%d",&balls);
if(balls<1)
printf("Must drop at least one ball ");
}while(balls<1);
return balls;
}
void getslot(int slotcount[])
{int dir,slot,j;
float x;
slot=0;
for(j=0;j<10;j++)
{x=rand();
dir=(int)x;
dir%=2;
if(dir==0)
slot--;
else
slot++;
}
slotcount[slot+10]++;
return;
}
void print(int slotcount[])
{int i,j;
for(i=-10;i<12;i+=2)
{printf("%3d:",i);
for(j=0;j<slotcount[i+10];j++)
printf(" o");
printf(" ");
}
return;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.