Problem with vector ordering

Asked

Viewed 87 times

1

Good afternoon, I’m having a problem with a vector ordering exercise, I’ve reviewed the code, it compiles, but I’m not finding the error so the order does not get crescent, varying in certain parts. Follow the code below:

#include <stdio.h> //Declaracao de bibliteca para entradas e saidas de valores

int main (void) //Declaracao do programa principal
{
    int i, j, troca, vetorA[10];
    for (i = 0; i < 10 ; i++)
    {
        printf("Digite o valor do elemento:");
        scanf("%d", &vetorA[i]);
    }
    for(i=0; j<10; i++)
    {
        for (j=i+1; j<10; j++)
        {   
            if(vetorA[i]>vetorA[j])
            {
                troca=vetorA[i];
                vetorA[i] = vetorA[j];
                vetorA[j] = troca;
            }
        }
    }
    printf("\nvetor ordenado \n");
    for(i=0; i<10; i++)
    {
        printf("%d - ", vetorA[i]);
    }
    system("pause");
    return 0;
}

2 answers

3


instead of

for(i=0; j<10; i++)

I thought you wanted to say

for(i=0; i<10; i++)

3

I believe it was just a typo:

#include <stdio.h>

int main (void) {
    int troca, vetorA[10];
        for (int i = 0; i < 10; i++) {
        printf("Digite o valor do elemento:");
        scanf("%d", &vetorA[i]);
    }
    for (int i = 0; i < 10; i++) { // <=========== Aqui tinha um j < 10 que obviamente causa confusão
        for (int j = i + 1; j < 10; j++) {  
            if (vetorA[i] > vetorA[j]) {
                troca = vetorA[i];
                vetorA[i] = vetorA[j];
                vetorA[j] = troca;
             }
        }
    }
    printf("\nvetor ordenado\n");
    for (int i = 0; i < 10; i++) {
        printf("%d - ", vetorA[i]);
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

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