samedi 20 février 2016

Sorting array in C

I'm trying to sort an array size in C, i have ran some tests on it before i publish it here (I have left the testing statement in). I get the correct answer once but than it moves the higher value into the wrong array, even though i have used an IF statement.

If you guys run the program it will make sense to you.

#include <stdio.h>
# define size 3

int sum();

main() {
    int a[size] = { 4, 2, 3 };
    int temp, i, j, x;

    for (j = 0; j < size; j++){

        for (i = 1; i < size; i++){
            for (x = 0; x < size; x++){
                printf("%d", a[x]); //testing statement; 
            }
            printf("\n");
            if (a[j] > a[i]){
                temp = a[j]; 
                a[j] = a[i];
                a[i] = temp;

            }

        }

    }


for (x = 0; x < size; x++){
            printf("%d", a[x]);
        }
    getch();
}

Aucun commentaire:

Enregistrer un commentaire