How could I arrange this code for it to print the numbers of the vector increasingly?

Asked

Viewed 45 times

0

    //Questão 4
#include <stdio.h>
#define TAM 10
int main()
{
    int i, valorVET, VET[TAM];
    printf("Digite os 10 numeros:\n");
    for(i = 0; i < TAM; i++)
    {
        scanf("%d", &VET[i]);
    }
    for(i = TAM - 1; i >= 0; i--)
    {
        if(i != 0)
        {
            if(VET[i] <= VET[i - 1])
            {
                valorVET = VET[i];
                VET[i] = VET[i - 1];
                VET[i - 1] = valorVET;
            }
        }
    }
    for(i = 0; i < TAM; i++)
    {
        if(i != 0)
        {
            if(VET[i] > VET[i + 1])
            {
                valorVET = VET[i];
                VET[i] = VET[i + 1];
                VET[i + 1] = valorVET;
            }
        }
    }
    printf("\n");
    for(i = 0; i < TAM; i++)
    {
        printf("%d ", VET[i]);
    }
    printf("\n\n");
    return 0;
}
  • Make a mistake ? Don’t order as you want ?

  • No mistake, just not ordering in the ascending order

  • 2

    To do the ordering you need a for within another, that is to say fors nested, this is following simple sorting algorithms like the Selection Sort or Bubble Sort

  • And how would that look? Can you tell me?

  • I would put the other for comparison within the first or do another for logic different?

  • I suggest you take the principle and follow the idea of the algorithm, getting a good idea of how it works, and then move on to the implementation. There is for example here on wikipedia references to the Bubble Sort, which is the most similar to what I was doing in the code. It also has other different sorting algorithms with their particularities

  • All right, thank you very much!!

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.