Bouncing-ball simulation I posted a couple questions about a week ago, but have almost gotten it working. This program is supposed to simulate the following: 


Ask the user how many balls to drop
Drop one ball at a time
Allow it to bounce ten times moving either once to the right or once to the left each bounce
Then print out the even numbers (since a ball could never land on an odd) -10 - 10 with a 'o' to represent each ball.




#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;time.h&gt;

/* Prototype for drop_Balls, int parameter is number of balls being dropped */
void drop_balls(int); 

/* Gets the number of balls that need to be dropped by the user */
int get_num_balls(); 

int main()
{
    drop_balls(get_num_balls());
}

int get_num_balls()   
{
    int num_balls;
    printf("How many balls should be dropped? ");
    scanf("%d", &amp;num_balls);
    /* Ensure that it is atleast one ball */
    while(num_balls &lt;= 0) 
    {
        printf("You have to drop at least one ball! \n ");
        printf("How many balls should be dropped? ");
        scanf("%d", &amp;num_balls);
    }
    /* Return the number of balls that will be dropped */
    return num_balls; 
}

void drop_balls(int num_balls)
{
    /* Keeps track of how many balls landed where */
    int ball_count[21]; 
    /* What number ball are we on */
    int ball_num = 0; 
    /* Seed the generator */
    srand(time(NULL)); 
    /* Do the correct # of balls */
    for(ball_num = 0; ball_num &lt; num_balls; ball_num++ ) 
    {
        /* Start at 10 since its the middle of 21 */
        int starting_point = 10; 
        /* Bounce each ball 10 times */
        for(starting_point = 10; starting_point &gt; 0; starting_point--) 
        {
           int number;
           /* Create a random integer between 1-100 */
           number = rand() % 100; 
            /* If its less than 50 bounce to the right once */
           if(number &gt;= 50) 
           {
               starting_point++;
           }
           /* If its greater than 50, bounce to the left once */
           else 
           {
               starting_point--;
           }
        }
        /* Add one to simulate one ball landing there */
        ball_count[starting_point]++;  
    }

    /* Start at the -10 spot */
    int ball_place = -10; 
    int x;
    /* Go through the 21 spots, but skip the odd ones, since they never get balls */
    for(x = 0; x &lt; 20; x+2)
    {
        printf("\n %d: ", ball_place);
        int l = 0;
        /* Print out an 'o' for each ball */
        for(l = 0; l &lt; ball_count[x]; l++)
        {
            printf("o");
        }
        /* Increase the spot, taking it from -10 - 10 */
        ball_place = ball_place + 2;
    }
}
Comments

You should use // comments for single-line comments.

I am not sure you are using comments properly. Comments shouldn't explain how, it should explain why.

/* Prototype for drop_Balls, int parameter is number of balls being dropped */
void drop_balls(int); 


One can see that this is a prototype. void drop_balls(int number_of_balls); would probably provide as much information in a more compact way.

/* Return the number of balls that will be dropped */
return num_balls; 


This does not look like a useful comment to me.

/* Keeps track of how many balls landed where */
int ball_count[21]; 


A bit of explanation on the 21 here would be useful... but it is not provided.

This leads me to the next section:

Magic numbers

You probably don't want to have hard-coded numbers all over the place. You could create a constant with value 21 and then:


starting_point = 10 would become starting_point = N /2.
for(x = 0; x &lt; 20; x+2) would become for(x = 0; x &lt; N; x+2).
int ball_place = -10; would become int ball_place = - N/2;


Code organisation

It would probably be better now to call srand in rand_balls as it shouldn't be its responsibility. You can do it from main() if you want.

Define variable in the smallest possible scope. For instance:

for(int ball_num = 0; ball_num &lt; num_balls; ball_num++ )


Also, how is this loop:

for(starting_point = 10; starting_point &gt; 0; starting_point--)


supposed to behave?  Don't we always have the variable set to 0 or -1 at the end?