# Thread: Completely new to programming: Having issues with arrays

1. ## Completely new to programming: Having issues with arrays

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
Code:
`rand()`
.

1) Calculate a random number between 0 and 1. To do this use
Code:
`rand()`
and divide by
Code:
`RAND_MAX`
. Remember to use a
Code:
`CAST`
for
Code:
`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 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
Code:
`for(){} loop`
).

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. 2. In your first example in need to include <time.h>; you must use "%d" to print each bin value (bin is an array of int, not double), and the index generated by your expression is (almost) always 0. 3. bins[(int)(rand()/(double) RAND_MAX)

rand() / (double)RAND_MAX will be a floating point number between 0 and 1. then when you cast it to int it will turn into 0 as the fraction is truncated.
instead of &'ing it by MAXBINS, try multiplying that value by MAXBINS, BEFORE casting to int, then cast the whole thing to int 4. Looks like qny and dmh2000 already gave you the fixes you need to actually make this work. A few extra suggestions for you, since you are a beginner:

• Programming is not about squeezing as much code onto a single line as possible. Put each statement onto it's own line. This helps your code be organized, which means it's easy to read/understand and harder to make silly mistakes. Popular pages Recent additions 