lundi 7 mai 2018

Dynamic array with random numbers

I'm new in C language and first time post on stackoverflow, I'm sorry for my mistakes. So I create Array with size N in cycle "for" and fill up random numbers.I have startPoint which means first size of array. endPoint it's end size, but step is step of size :)

for(int n = startPoint; n <= endPoint; n += step)
{
    int * Array = malloc(sizeof(int)*n);//Create dynamic array wit size n

    generationNumber(Array,n);// Function which generate random numbers and write to array

    printArray(Array,n);//Here I print array to cmd

    Executing(Array,n);//Here I execute some action with array, it's not important  

    free(Array);
}

So my problem is in results: enter image description here

It's function generationNumber:

void generationNumber(int * Array, int n)
{
     srand((unsigned)time(NULL));

     for(int i = 0; i < n; i++)
     {
        Array[i] = rand()%1000;
     }

}

Why numbers are repeated? I feel that just add numbers to array.




Aucun commentaire:

Enregistrer un commentaire