Numbers in Ascending Order C

Asked

Viewed 25,163 times

1

I am trying to make a code to show 3 random numbers in ascending order typed by the user however I do not understand why my code is not working

#include <stdio.h>

main(){

int numeros[3],a,b,c,maior=0,menor=0,medio=0;



printf("Digite 3 numeros:\n");
scanf("%d %d %d",&numeros[0],&numeros[1],&numeros[2]);


for(a=0;a<3;a++){
    if(numeros[a]>numeros[a+1]){
        numeros[a]=maior;
    }


}
for(b=0;b<3;b++){
    if(numeros[b]<numeros[b+1]){
        numeros[b]=menor;
    }
}
for(c=0;c<3;c++){
    if(numeros[c]<maior && numeros[c]>menor){
        numeros[c]=medio;
    }
}

printf("%d %d %d",menor,medio,maior);

output appears only 0 in all 3 positions.

  • If your intention is just to put in the variables maior, medio and menor, I believe you reversed it. You seem to be placing the value of the largest variable, which was not initialized, in the number line [a]=higher. Try switching to larger = numbers[a].

  • It still doesn’t work anyway

  • If you wish, I wrote an answer to order 8 numbers without using vectors or loops. I also give an idea of why it was done that way and some theoretical basis in the subject: https://answall.com/a/240531/64969

2 answers

5

This code is too complex, it doesn’t need all this and I found the answer accepted even worse because it became a quadratic algorithm (not that it makes much difference in such a simple case), so here is a simpler solution. If you want to use array even, which is unnecessary, just change the variables. If you want to make a loop gives also, but it gets more complicated and so has no sense.

#include <stdio.h>

int main() {
    int a, b, c;
    printf("Digite 3 numeros:\n");
    scanf("%d %d %d", &a, &b, &c);
    if (a > c) {
        int tmp = c;
        c = a;
        a = tmp;
    }
    if (a > b) {
        int tmp = b;
        b = a;
        a = tmp;
    }
    if (b > c) {
        int tmp = c;
        c = b;
        b = tmp;
    }
    printf("%d %d %d", a, b, c);
}

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

It is possible to generalize a function of swap to avoid these 3 exchange lines, but in such a simple example does not even compensate.

I always prefer to use the Razor of Occam.

1


You are checking errors?

For ifs will give error for popping the array at "[a+1]" and "[b+1]": numeros[a]>numeros[a+1] and numeros[a]>numeros[b+1].

You reversed the value assignment:

numeros[a]=maior;
numeros[b]=menor;
numeros[c]=medio;

Change to:

maior=numeros[a];
menor=numeros[b];
medio=numeros[c];

You can do it that way too:

int i, j, a, n, number[30];

printf("Enter the value of N \n");
scanf("%d", &n);
printf("Enter the numbers \n");
for (i = 0; i < n; ++i)
    scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{
    for (j = i + 1; j < n; ++j)
    {
        if (number[i] > number[j])
        {
            a =  number[i];
            number[i] = number[j];
            number[j] = a;
        }
    }
}
printf("The numbers arranged in ascending order are given below \n");
for (i = 0; i < n; ++i)
    printf("%d\n", number[i]);

Source: http://www.sanfoundry.com/c-program-sort-array-ascending-order/

  • "Strange" we have downvote on an accepted question! haha

  • I think to make it clear that here one is rotating one selection sort be positive for a future reader

Browser other questions tagged

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