Hi guys, first things first, I am not looking for someone to write all this up for me I am jut really stuck and won't be able to get help from tutors until term starts again in October. I was in hospital in term time but have been allowed to keep studying as long as I do it over the summer. I figured you guys might be able to help me out.
The first bit of the assignment is as follows:
Write a program to calculate the distribution of numbers produced by the random number function
.
1) Calculate a random number between 0 and 1. To do this use and divide by
. Remember to use a for 2) Next write some code to ‘bin’ the data. Create an array where each element represents a bin
(use, say, 10 bins to start but make this variable). 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 ).
I feel like I'm almost there with this one. I have two seperate programmes I am trying this with, on the suggestions of other people who have given me a hand.
The first is as follows:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(){
const int MAXBINS = 10, MAXITERS = 1000; //declares the number of bins and the number of of the numbers.
int bins[MAXBINS], i;
srand(time(NULL)); //ensures a different answer is given each time the program is run.
for (i=0; i<MAXBINS; ++i) bins[i] = 0;
for (i=0; i<MAXITERS; ++i) bins[(int)(rand()/(double) RAND_MAX) &MAXBINS]++;
for (i=0; i<MAXBINS; ++i) printf("bin %d has count %f\n", i, bins[i]);
return 0;
}
This code gives me the bin numbers and a corresponding 'value' the issue being all the values are 0.
And the second method:
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double x;
int i;
for(i=0;i<9;i++)
{
x = rand()/(double) RAND_MAX;
printf("%f\n",x);
}
if ((0 <= x) && (x < 0.1)) { /* statements */ }
else if ((0.1 <= x) && (x < 0.2)) { /* statements */ }
else if ((0.2 <= x) && (x < 0.3)) { /* statements */ }
else if ((0.3 <= x) && (x < 0.4)) { /* statements */ }
else if ((0.4 <= x) && (x < 0.5)) { /* statements */ }
else if ((0.5 <= x) && (x < 0.6)) { /* statements */ }
else if ((0.6 <= x) && (x < 0.7)) { /* statements */ }
else if ((0.7 <= x) && (x < 0.8)) { /* statements */ }
else if ((0.8 <= x) && (x < 0.9)) { /* statements */ }
else ((0.9 <= x) && (x < 1.0)) { /* statements */ }
return 0;
}
So the statements will be the bin declarations, but again, not sure how to do that.
Thanks in advance for your time spent helping me, it means the world.