Note that in your second loop, you are going to get a segfault since occur has only 6 spaces, but you are looping all the way to i=20. Woops! I think what you meant to say there was:
Code:
for (i = 0; i < 10; i++) // This should be for (i = 0; i < 20; i++), typo on my part
{
if (random[i] == 6)
occur[5]++;
}
Note that it is 5, and not 6, since a 6 element array has indices 0,1,2,3,4,5
If you want to use occur[i] to store the number of occurences of i in random, you are going to need 7 entries in occur because arrays in C start at 0.
If you do that, why not count all occurences of all the numbers in one go?
Code:
for (i=0; i<20; i++)
{
occur[random[i]]++;
}
Should work, as long as occur has 7 entries. You can always just ignore the zero entry. Or if you prefer not to waste space:
Code:
for (i=0; i<20; i++)
{
occur[random[i]-1]++;
}
Then occur[j] stores the number of occurences of j+1.
Great job so far though. Keep it up
PS: make sure you intitialize occur[] to all 0's before the loop. I forget offhand if C does that for you when you declare an array as you did, but it can't hrt to program defensively