samedi 2 février 2019

My shuffle function apparently doesn't work without a printf

I was trying to create a little function to shuffle the values of an integer array. (I am working on CodeBlocks 17.12, if that can help)

Firstly i wrote a little function (shuffle1) but, seeing that my program kept crashing, i changed it a bit (shuffle2) and i noticed that my program doesn't crash if i put a printf in the for loop, even if it doesn't print anything.

void swap(int* a, int* b){
    if(a != b){
        int *tmp;
        *tmp = *a;
        *a = *b;
        *b = *tmp;
    }
}


void shuffle1(int* v, int dim){
    srand(time(NULL));

    for(int i=0; i<dim; i++){
        swap(&v[rand()%dim], &v[rand()%dim]);
    }
}


void shuffle2(int* v, int dim){
    srand(time(NULL));
    int x,y;
    int end = rand()%dim +dim;
    for(int i=0; i<end; i++){
        x = rand()%dim;
        y = rand()%dim;
        printf("",x,y);
        swap(&v[x], &v[y]);
    }
}




Aucun commentaire:

Enregistrer un commentaire