Hi
I have this code but i can't write the numbers sorted, what's wrong??
I want to have 4 numbers random, this is working, then i want another printf with this 4 numbers sorted.Code:#include <stdio.h> #include <stdlib.h> #include <time.h> #define TAM 100 void LeVetor(int num[]) { int i, n, tmp; srand(time(NULL)); // Inicia o array for(i = 0;i < TAM;++i) num[i] = i; // Embaralha o array for (i = TAM; i > 1;) { n = rand() % i; --i; tmp = num[n]; num[n] = num[i]; num[i] = tmp; } // Iteração através do array. Os números já estão random for(i = 0;i < 4;++i) printf("%d\n", num[i]); } void OrdenaVetor(int num[]) { int aux, i=0, j=0; for(i=0; i<TAM; i++){ for(j=0; j<TAM; j++) { if(num[i]<num[j]) { aux=num[i]; num[i]=num[j]; num[j]=aux; } } } } int main(void) { int num[TAM]; int i; LeVetor(num); printf("\n \n \n"); OrdenaVetor(num); for(i=0; i<4; i++) { printf("%d", num[i]); } }
What's wrong with this, i have
Random number
37
94
38
5
Sorted
0123
it should be
5
37
38
94
any help?
Thnaks