Problem with program that prints three numbers incrementally

4

I'm having trouble creating decision structures in a program that reads 3 numbers and prints them incrementally. My code that is going wrong is as follows:

#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?

    
asked by anonymous 27.03.2015 / 02:21

3 answers

4

This may not be the best way to do this but as I think you are learning I will not try to mess around with your logic. I will solve two problems in it.

The first is that you can not make a complex comparison. The computer works like our brain, it does one operation at a time. He can not 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 make another comparison and then do another one that brings the two together. This operation that will join the two is the "logical AND", that is, both operations must be true for everything to be true. In C the operator responsible for this is && .

The second problem is that you have not checked all order possibilities.

There is another problem that the code is poorly organized perhaps because you do not know the else if that executes if the previous condition is false. But she already sees another condition. You can do without else if , just delete all else s and make if s independent. Still it has how to do of another way and more optimized (only to reorder the sequence of each condition already would optimize a little), but I will not complicate for you. I think I'm already introducing several new concepts.

It 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) { //Se A for menor que B e se B for menor que C, A é o menor e C é o maior
       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);
    }
    return 0;
}

See working on ideone .

    
27.03.2015 / 02:49
2

There is a problem with logic, because as you said yourself, you are not covering all the possibilities. In addition there is a semantic problem in:

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

It is not possible to do this direct comparison, the comparison should be done two by two, this way:

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

A solution to your problem is to create a vector of elements, sort it and then print its elements. Thus, for a vector of n elements, the code 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 for loop to print 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, as there are several other algorithms that perform the same task faster.

    
27.03.2015 / 03:02
1

This code does a funny thing.

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

First it compares A and B to get 0 or 1 ; then compares this value with C

 if ((A < B) < C) /* ... */;
 if (0 < C) /* ... */;
 if (1 < C) /* ... */;
    
27.03.2015 / 09:01