3
I am ordering a simple vector, increasingly. The code works.
But the next line that got me confused:
if (matriz[i] < matriz[j])
Why is the sign there smaller, not bigger? If I want to change positions, if the position i
is greater than the position j
, because the sign is contrary to this?
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
int main()
{
int k = NULL, *matriz = NULL, aux=0;
printf("Tamanho do vetor: ");
scanf("%i", &k);
matriz = (int *)malloc(k * sizeof(int));
srand(time(0));
printf("\n");
printf("NAO ORDENADO: ");
printf("\n");
for (int i = 0; i < k; i++)
{
matriz[i] = rand() % 100;
printf("Posicao %d: %d", i + 1, matriz[i]);
printf("\n");
}
for (int i = 0; i < k; i++)
{
for (int j = 0; j < k; j++)
{
if (matriz[i] < matriz[j])
{
aux = matriz[i];
matriz[i] = matriz[j];
matriz[j] = aux;
}
}
}
printf("\n");
printf("ORDENADO: ");
printf("\n");
for (int i = 0; i < k; i++)
{
printf("Posicao %d: %d", i + 1, matriz[i]);
printf("\n");
}
printf("\n");
printf("\n");
system("pause");
return 0;
}
It calls it vector: a unidimencional matrix is like having a dog called cat :)
– JJoao