mercredi 17 novembre 2021

How can i actuate swap func in loop?

for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4 - i; j++) {
        if (array[j] > array[j + 1]) {
            swap(array[j], array[j + 1]);
        }
    }
}

printf("Descending : ");
for (int k = 4; 0 <= k; k--) {
    printf("%d ", array[k]);
}

I made code for descending five typed integer numbers. And there was two ways for activating this. The first one is using temp swap in loop(like, tmp=a, a=b, b=tmp), and second one is declaring swap function(void swap(int x, int y){int tmp=x, x=y, y=tmp}) and using this func in loop. First way was successful, but the second way was failed.

I want to know why swap func in loop can't be actuated.

Aucun commentaire:

Enregistrer un commentaire