Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your program will \"drop balls\" each of which will bounce randomly and land in

ID: 3629786 • Letter: Y

Question

Your program 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 and 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.

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;
}