-
Random Number Issues
I am very new to C and have been tasked to produce a program that generates 1000 random numbers (with a max of 9) and then finds the total average. I have produced the following but it only seems to return 0 as the answer.
Code:
// Program to determine the mean of 1000 random numbers //
// written by Kerim Morris 01/10/2010 //
#include <stdlib.h>
int max_count = 1000; //declare the variables//
int count = 0;
int random_number = 0;
int random = 0;
int random_total = 0;
float mean = 0;
main() //start of main function//
{
{
for (count = 0; count < max_count; count ++); //loop counter to a maximum//
//of 1000 //
random_number = (int)(10.0*rand()/(RAND_MAX+1.0)); //produce random//
// numbers between 0-9//
random_total = random_number + random_total; //calculate the running //
// total//
}
mean = (random_total / max_count); //calculate mean of total //
printf("The mean of 1000 random numbers equals %f");
}
Any guidance would be appreciated.
-
Code:
for (count = 0; count < max_count; count ++); //loop counter to a maximum//
-
I'm not quite sure what you mean, you seem to have repeated the same code I have but unfortunately there is no explanation.
-
Yeah I think his blue semi-colon where there shouldn't be a semi-colon isn't that visible.
-
Code:
for (count = 0; count < max_count; count ++); //loop counter to a maximum//
There, that's a bit more obvious I think. At least when you've figured out that upside down, slightly off, italic exclemation point is in fact a semicolon.
-
1) The semicolon isn't supposed to be there is their way of saying...
Put opening brace instead.
2) Also you have en extra opening-brace after main (one too many).
3) This is incorrect:
Code:
mean = (random_total / max_count);
. You need to do
Code:
mean = ((float)random_total / max_count);
4) You need to fix your printf so that ot outputs mean.