Vector ordering problem in C

Asked

Viewed 221 times

-1

I’m trying to make this problem, but always the result goes wrong.

"Make a program that creates a user-filled 10-position array. Scan the vector and whenever the value of position i of the vector is less than the value of position i+1 you must change them."

#include <stdio.h>

int main()
{

    int i,vet[10],aux;
    aux = 0;

    for(i=0;i<10;i++)
    {
        printf("Digite um valor:\n");
        scanf("%d",&vet[i]);
    }

    for(i=0;i<9;i++)
    {

        if(vet[i]<vet[i+1])
        {   

            aux = vet[i];
            vet[i] = vet[i+1];
            vet[i+1] = aux;


        }   
    }


    printf("\n");
    for(i=0;i<10;i++)
    {
        printf("%d\n",vet[i]);

    }

return 0;
}
  • You are checking your vector only once. If there is any change you have to check the whole vector again, until no change occurs. So we missed a loop.

2 answers

0


Are you trying to sort using the correct bubble method? , so would the program (in ascending order):

but rather the summary on ordering: Example: a=10 b=5 , I can’t change their values to simply sort through by saying that a=b or b=a because I lose the values, so I need a helper to store the value to be exchanged. If I were to put it in ascending order:

1-I’ll change the value of b to the

auxiliary=b b=a a=auxiliary

result: a=5 b=10

*watch out for variable names so there’s no trouble up ahead ^^

#include <stdio.h>

int main()
{
    int i;
    int j;
    int valor[10];
    int auxiliar;

    for(i=0; i<10; i++)
    {
        printf("Digite um valor:\n");
        scanf("%d",&valor[i]);
    }
    for(i=0; i<10; i++)
    {
        for(j=i+1; j<10; j++)
        {
            if(valor[i]>valor[j])
            { 
                auxiliar=valor[i];
                valor[i]=valor[j];
                valor[j]=auxiliar;
            }
        }
    }

    printf("\n");
    for(i=0; i<10; i++)
    {
        printf("%d\n",valor[i]);
    }
    return 0;
}
  • Sorry to edit so much , I tried to complement to the maximum !

  • Since the code of the question is indented yours should also be, because it facilitates reading.

  • yes, unfortunately I can’t indent in this forum , I don’t know if it’s a bug but typing space 5 thousand times is not enough!

  • 1

    When you copy the code from the editor and it already indents from the editor then you don’t have to do anything. Just apply code formatting by selecting the code and clicking the button {} from the response editor or by pressing Ctrl + k.

0

Another way to do it is by using the XOR Swap algorithm where you can exchange values without using an auxiliary variable.

int main() 
{

    int i, vet[10], aux;
    aux = 0;

    for (i = 0; i < 10; i++)
    {
        printf("Digite um valor:\n");
        scanf_s("%d", &vet[i]);
    }

    for (i = 0; i < 10; i++)
    {
        if (vet[i] < vet[i + 1])
        {
            // Operação SWAP XOR
            vet[i] ^= vet[i + 1];
            vet[i + 1] ^= vet[i];
            vet[i] ^= vet[i + 1];
        }
    }

    printf("\n");
    for (i = 0; i < 10; i++)
    {
        printf("%d\n", vet[i]);
    }

    return 0;
}

If you want to know a little more, have a reference here

  • As you compare element i with i+1 then your loop should be 0 through 8, not up to 9. for (i = 0; i < 9; i++)

Browser other questions tagged

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