Question

I'm developing in C using OpenVMS, I've done a code that put in a 1001 (0-1000) elements array, 1000 (0-999) random numbers between 0 and 50. Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main(){
    int vet[1000], fre[50];
    int i;

    srand(time(NULL));

    for(i=0;i<1000;i++){
        vet[i]=(rand()%51);
    }

    for(i=0;i<1000;i++){
        printf("%d\n", vet[i]);
    }

    for(i=0;i<1000;i++){
        fre[vet[i]]=fre[vet[i]]+1;
    }

    for(i=0;i<51;i++){
        printf("The number %d  was generated %d times\n", i, fre[i]);
    }
}

When I show how much times each number was generated, I saw that the number 50 has a big number, sometimes more than double than others numbers, someone can help me?

SOLVED Code that works I must use srand() for now

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

main(){
        int vet[1000], fre[51] = {0};
        int i;

        srand(time(NULL));

        for(i=0;i<1000;i++){
                vet[i]=(rand()%51);
        }

for(i=0;i<1000;i++){
printf("%d\n", vet[i]);
}

        for(i=0;i<1000;i++){
                        fre[vet[i]]=fre[vet[i]]+1;
        }

        for(i=0;i<51;i++){
                printf("The number %d  was generated %d times\n", i, fre[i]);
        }
}
[EOB]

Thank you all

Was it helpful?

Solution

    int vet[1000], fre[50];


    for(i=0;i<51;i++){
            printf("The number %d  was generated %d times\n", i, fre[i]);
    }

Problem 1: You declare fre to have 50 elements, but you use 51.

Problem 2: fre isn't initialised.

int vet[1000], fre[51] = {0};

should give you reasonable output.

OTHER TIPS

int vet[1000], fre[50];

Your vet[] array has 1000 entries, not 1001. fre[] has 50, not 51. If you are generating numbers from 0 to 50, you need to declare fre[] as fre[51];

You also never clear your fre[] array before accumulating the results.

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top