jeudi 1 novembre 2018

DIfference in output of srand() on windows gcc and expected output for C programming?

i would like to know why there is a difference in the output from my gcc compiler in Sublime Text 3 on Windows 10 and the expected output? If so how can i change the compiler such that it operates the same?

This is the expected output, which i can receive when utilising https://repl.it/repls/InternalSeveralEntropy with gcc 4.6.3

The set of numbers are:

1 4 3 3 7 2 3 2 9 9 10 9 5 2 10 7 2 3 1 1 

However, this is the output i receive using multiple gcc versions, 5.4.0, 6.3.0, 8.1.0 and even 4.6.3.

The set of numbers are:
2 2 8 6 8 2 3 8 4 6 10 5 9 2 8 10 7 2 1 6

I have tried searching and the closest i have come to understand is that it has soemthing to do with srand() functioning differently? Attached below is my code.

    #include <stdio.h>

    #include <stdlib.h>

    int main(void) {

    int N = 20, DATA[20];

    int i; //< you local variables here >

    srand(454646); //don't change this line! Will be used in autograding... You may fail test cases if change this

    for (i = 0; i < N; i++){        //< generate rand numbers and store here in DATA array >
        DATA[i] = rand()%10 + 1;
    }

    printf("The set of numbers are:\n");
    for (i = 0; i < N; i++){
        printf("%d ", DATA[i]);
    }
    printf("\n");

    //< write using FOR loops to determine MODE and print >

    return 0;

}




Aucun commentaire:

Enregistrer un commentaire