Can you go over my code please?

I am not sure what I did wrong

#include <stdio.h>
#include <stdlib.h>
int main()
{
int array[1000];

int sum[6]={0,0,0,0,0,0};
for(int i=0; i < 100 ; i++)
{
array[i] = (int) (rand() % 6 + 1);
sum[array[i]] = sum[array[i]] + 1;
}
for(int i=0; i<6; i++)
{
printf("\n%d occurs %d times",(i+1),sum[i]);
}
return 0;
}
What makes you think you did something wrong? What is different from what you were expecting?
^ Next time, think about that and answer it preemptively in your original post.

But for now, it looks like the issue is an understanding of array indices.
An array of size 6 has indices 0, 1, 2, 3, 4, 5.

array[i] = (rand() % 6 + 1) produces numbers in range [1, 6].
So when you do sum[array[i]], you have a 1/6 chance of going out of bounds, i.e. sum[6].
Last edited on
ok thank you
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
You're not seeding the random number generator by calling srand(). That means you're going to get the same sequence of pseudo-random numbers every time you run your program. You should call srand() once at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/
Topic archived. No new replies allowed.