1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare(void const *a, void const *b)
{
int const * first = (int*)a;
int const * second = (int*)b;
return *first - *second;
}
int main(void) {
printf("Veuillez saisir des nombres (dernier vaut 0) \n");
int rep,j,i=0;
int* tab =malloc(sizeof(int));
scanf("%d",&rep);
while (rep!=0) {
tab = realloc(tab,i * sizeof(int));
tab[i] = rep;
i++;
scanf("%d",&rep);
}
printf("Tableau non trie : ");
for(j=0;j<i;j++) {
printf("%d ",tab[j]);
}
printf("\n");
qsort(tab,i,sizeof(int),compare);
printf("Tableau trié : ");
for(j=0;j<i;j++) {
printf("%d ",tab[j]);
}
return 0;
} |