Problem with program that prints three numbers increasingly

Asked

Viewed 185 times

5

I’m having trouble creating the decision structures in a program that reads three numbers and prints them increasingly. My code that’s going wrong is this::

#include <stdio.h>

int main (void)
{
    int A, B, C;

    scanf("%d %d %d" ,&A, &B, &C);

    if(A<B<C)
    {
       printf("%d %d %d" ,A, B, C);
    }      
      else
    {
        printf("%d %d %d", C, B, A);

        if(B<A<C)
        {
            printf("%d %d %d", B, C, A);
        }
    }

return 0;

}

What should I do to test all possibilities?

3 answers

6


This may not be the best way to do this but as I think you’re learning I won’t try to mess with your logic too much. I will solve two problems existing in it.

The first is that you can’t make a complex comparison. The computer works like our brain, it does one operation at a time. It cannot buy if 3 numbers one is smaller than the other. You can only compare two numbers at a time. You make a comparison and then you make another comparison and then you make another one that brings the two together. This operation that will join the two is the "And logical", that is, both operations must be true for everything to be true. In C the operator responsible for this is the &&.

The second problem is that you haven’t checked all the order possibilities.

Is there another problem that the code is poorly organized perhaps because you don’t know the else if which it performs if the previous condition is false. But it already checks another condition. It can be done without the else if, just eliminate all the elses and make the ifs independent. Still you can do it differently and more optimized (just reordering the sequence of each condition would already optimize a little), but I will not complicate for you. I think I’m already introducing several new concepts.

Would look like this:

#include <stdio.h>

int main (void) {
    int A, B, C;
    scanf("%d %d %d", &A, &B, &C);
    if (A < B && B < C) printf("%d %d %d", A, B, C);
    else if (C < B && B < A) printf("%d %d %d", C, B, A);
    else if (B < A && A < C) printf("%d %d %d", B, C, A);
    else if (A < C && C < B) printf("%d %d %d", A, C, B);
    else if (B < A && A < C) printf("%d %d %d", B, A, C);
    else if (C < A && A < B) printf("%d %d %d", C, A, B);
}

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

3

There is a problem in logic, because as you said yourself, you are not covering all the possibilities. Also there is a problem of semantics in:

if(a<b<c)
{
    //seu código
}

It is not possible to make this direct comparison, the comparison should be made two by two, like this:

if((a<b) && (b<c)) // && significa /\ (o "e" em lógica)
{
    //seu código
}

One solution to your problem is to create an array of elements, order them, and then print their elements. Thus, for a vector of n elements the codex is as follows::

void ordenavetor(int vet[], int n)
{
    int i,j,aux;

    for(i=0;i<n-1;i++)
        for(j=i+1;j<n;j++)
        {
            if(vet[i]>vet[j])
            {
                aux = vet[i];
                vet[i] = vet[j];
                vet[j] = aux;
            }
        }
}

Then create a bond for to print the vector elements:

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

This is just one of the solutions, because there are several other algorithms that perform the same task faster.

1

This code does a funny thing.

if (A < B < C) /* ... */;

First he compares A and B getting 0 or 1; then compare this value with C

 if ((A < B) < C) /* ... */;
 if (0 < C) /* ... */;
 if (1 < C) /* ... */;

Browser other questions tagged

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