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 <stdio.h>
#include <stdlib.h>
#include <time.h>
/* 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", &num_balls);
/* Ensure that it is atleast one ball */
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 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 < 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 > 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 >= 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 < 20; x+2)
{
printf("\n %d: ", ball_place);
int l = 0;
/* Print out an 'o' for each ball */
for(l = 0; l < ball_count[x]; l++)
{
printf("o");
}
/* Increase the spot, taking it from -10 - 10 */
ball_place = ball_place + 2;
}
}
-
1\$\begingroup\$ So, what's the question? Is your code working the way you want it to? If not, then your question is off topic here. \$\endgroup\$svick– svick2013年10月07日 14:37:29 +00:00Commented Oct 7, 2013 at 14:37
2 Answers 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 becomestarting_point = N /2
.for(x = 0; x < 20; x+2)
would becomefor(x = 0; x < N; x+2)
.int ball_place = -10;
would becomeint 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 < num_balls; ball_num++ )
Also, how is this loop:
for(starting_point = 10; starting_point > 0; starting_point--)
supposed to behave? Don't we always have the variable set to 0 or -1 at the end?
-
\$\begingroup\$ I agree with what you said about the commenting, I will also go through and change them to make more sense once I get it working. The constants I know make it look sloppy, I was going to go through and change that once I got everything working. That loop moves it once to the left or right every pass through, so I think its doing what its supposed to. \$\endgroup\$Steve– Steve2013年10月07日 06:25:20 +00:00Commented Oct 7, 2013 at 6:25
-
1\$\begingroup\$ Can you explain your advice, "You should use
//
comments for single-line comments."? What if the OP needs the code to compile with the-ansi
flag? \$\endgroup\$Gareth Rees– Gareth Rees2013年10月07日 12:36:39 +00:00Commented Oct 7, 2013 at 12:36 -
\$\begingroup\$ Shouldn't
srand()
always be called inmain()
? It doesn't look like it should be an option. \$\endgroup\$Jamal– Jamal2013年11月10日 23:52:34 +00:00Commented Nov 10, 2013 at 23:52
In addition to @Josay's answer:
This code contains some duplication which you can avoid:
printf("How many balls should be dropped? ");
scanf("%d", &num_balls);
/* Ensure that it is atleast one ball */
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);
}
Either refactor for the number of balls into it's own method:
int ask_for_number_of_balls()
{
printf("How many balls should be dropped? ");
scanf("%d", &num_balls);
}
And then your loop becomes:
int num_balls = ask_for_number_of_balls();
while (num_balls <= 0)
{
printf("You have to drop at least one ball! \n ");
num_balls = ask_for_number_of_balls();
}
Or you get a bit clever:
int num_balls = -1;
while (1)
{
printf("How many balls should be dropped? ");
scanf("%d", &num_balls);
if (num_balls > 0) { break; }
printf("You have to drop at least one ball! \n ");
}
I would probably prefer the refactored method over clever (clever is harder to read and maintain).
I'm not sure about this loop:
for(starting_point = 10; starting_point > 0; starting_point--)
You randomly add one or subtract one from starting_point
, but the running condition for that loop is starting_point > 0
. So, as long as the starting point is greater than 0, this loop will run. This means that at the end, starting_point
will always be 0 afterwards (not to mention that your loop could run for a long time if your random numbers happen to be greater than 50 most of the time).