1) Initialize your variables before you use them:
int vet[1000] = {0};
int fre[50] = {0};
2) You're checking for values outside the array size:
for(i=0;i<51;i++){
should be:
for(i=0;i<50;i++){
Your array fre[50]
has elements fre[0]
through fre[49]
. So you want your count to start at 0 and go to <50
, ie 49.
3) You're generating numbers outside your array size:
vet[i]=(rand()%51);
should be:
vet[i]=(rand()%50);
rand() % x
will generate a number between 0-(x-1)
, if your array is size 50
then its elements are 0
-49
, which means you need to pick %50
or else you'll go over the array size when assigning with: fre[vet[i]]=fre[vet[i]]+1;
4) Keep in mind that rand() function generates pseudo-random output, so there's always the chance it won't be as "random" as you wanted.
EDIT
Ok, your comment: 2) No because I'm checking also 0
makes me think you don't understand how arrays work:
int fre[50] = {0};
gives you an array of 50 elements. The index of arrays start at 0 and go to [number of elements - 1], such that:
first element -->fre[0], fre[1], fre[2], ..., fre[48], fre[49] <-- last element
If you want to record values from 0 to 50 inclusive you need 51 elements in your array:
int fre[51] = {0};
So both of these:
for(int i=0; i<50; i++) and for(int i=0; i<51; i++)
start at 0, and run through each element, but the former works on fre[50]
(fifty elements from 0 to 49) and the latter works on fre[51]
(fifty one elements, from 0 to 50)