Ball Dropping Simulation Program, simulating a random direction bounce in
C? [on hold]
This is in C only, not C++
The entire program is supposed to ask the user how many balls to drop,
make sure that number is at least one. And then simulate dropping the
balls, and allowing them to "bounce" ten times each, and then keep track
in an array how many balls landed in each array. Each bounce allows the
ball to either go left once or right once. This is all I have so far, any
tips?
When I go to print out the final product it should look something like
this. With each 'o' representing one ball dropped in that place
-10:
-8:
-6: o
-4: ooooooooo
-2: oooooooooooooooooooooo
0: ooooooooooooooooooooooooo
2: oooooooooooooooooooooo
4: ooooooooooooo
6: ooooooo
8:
10: o
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int drop_balls(int); /* Prototype for drop_Balls, int parameter is number
of balls being dropped */
int get_num_balls(int); /* Gets the number of balls that need to be
dropped by the user */
int main()
{
get_num_balls();
drop_balls(num_balls);
}
int get_num_balls()
{
printf("How many balls should be dropped? ");
scanf("%d", num_balls);
while(num_balls <= 0)
{
printf("You have to drop at least one ball! \n ")
printf("How many balls should be dropped? ");
scanf("%d", num_balls);
}
return num_balls; /* Return the number of balls that will be dropped */
}
int drop_balls(num_balls)
{
int ball_count[21]; /* Keeps track of how many balls landed where */
int ball_num = 0; /* What number ball are we on */
srand(time(NULL)); /* Seed the generator */
for(ball_num; ball_num < num_balls; ball_num++ ) /* Do the correct #
of balls */
{
int starting_point = 10; /* Start at 10 since its the middle of 21 */
for(starting_point; starting_point >=0; starting_point--) /*
Bounce each ball 10 times */
{
int number;
number = rand() % 100;
if(number > 50)
{
starting_point++;
}
else
{
starting_point--;
}
}
ball_count[starting_point] += "o"
}
int j;
for(j = 0; j < 21; j++)
{
if(ball_count[j] != 'null')
{
printf("'%d :'", ball_count[j]);
}
}
}
}
No comments:
Post a Comment