Im a complete novice to C programming and have had a problem sheet given to me for the start of my second year physics degree. The question is this;
Write a program to calculate the distribution of numbers produced by the random number function rand().
1) Calculate a random number between 0 and 1. To do this use rand() and divide by RAND_MAX.
2) Next write some code to ‘bin’ the data. Create an array where each element represents a bin, (use, say, 10 bins to start). Increment (increase by one) the array element where the number falls. You need to calculate which bin the random number will fall into. If x is the random number between 0 and 1 and there are 10 bins, then the bin number will be the integer part of x*10 (assuming your array starts at 0).
Repeat this for a large number of times (use a for loop).
Calculate the normalised frequency of occurrence in each bin (just the number of bin elements divided by the total number of samples). Then display the results.
This is my code;
When I execute it, I get this;
for (b=0; b<30; b=b+1)
for (b=0; b<10; b=b+1)
printf("Count number in bin %d is %d\n", b,bin[b]);
Count number in bin 0 is 0
Count number in bin 1 is 0
Count number in bin 2 is 0
Count number in bin 3 is 0
Count number in bin 4 is 0
Count number in bin 5 is 0
Count number in bin 6 is 0
Count number in bin 7 is 0
Count number in bin 8 is 0
Count number in bin 9 is 0
which is obviously not what I want. I have been looking at this and fiddling with it for hours and hours, where have I gone wrong?
Thanks in advance.