Program in C: A certain doubt

2

The program I did compiled, but what is requested does not appear on the screen. The question is this:

  

Construct an algorithm in PORTUGOL, which receives three values, A, B, and C, and   store them in three variables with the following names: MAJOR, INTER and MINOR   (the names correspond to the ordered values).

#include <stdio.h>
#include <stdlib.h>
main()
{
  float a,b,c,maior,menor,inter;
  printf("\n Digite o primeiro valor:");
  scanf("%f",&a);
  printf("\n Digite o segundo valor:");
  scanf("%f",&b);
  printf("\n Digite o terceiro valor:");
  scanf("%f",&c);
  if((a<b)&&(a<c))
  {
  menor=a;
  }
  if(b<c)
  {
  inter=b;
  maior=c;
  }
  else
  {
  inter=c;
  maior=b;
  }
  if((b<a)&&(b<c))
  {
  menor=b;
  }
  if(a<c)
  {
  inter=a;
  maior=c;
  }
  else
  {
  inter=c;
  maior=a;
  }
  if((c<a)&&(c<b))
  {
  menor=c;
  }
  if(a<b)
  {
  inter=a;
  maior=b;
  }
  else
  {
  inter=b;
  maior=a;
  }
  system("pause");

  return 0;
}
    
asked by anonymous 09.10.2016 / 00:00

3 answers

2

Another very efficient solution is using For loop with selection structures applied inside the loop, this also allows the code to be smaller and more readable. At the end of the code I used the printf () function to return the value of the variables;

    #include <stdio.h>
    #include <stdlib.h>


    int main(){
        int i;
        float vetor[3], menor, inter, maior, valVetor;
        for(i = 0; i < 3; i++){
            printf("Digite o %dº valor:", i + 1);
            scanf("%f", &vetor[i]);
        }
        for(i = 0; i < 3; i++){
            valVetor = vetor[i];
            if(i == 0){
                menor = valVetor;
                inter = valVetor;
                maior = valVetor;
            }else{
                if(valVetor > maior){
                    inter = maior;
                    maior = valVetor;
                }else{
                    if(valVetor < menor){
                        inter = menor;
                        menor = valVetor;
                    }else{
                        inter = valVetor;
                    }
                }
            }
        }
        printf("Menor: %.2f\nIntermediario: %.2f\nMaior: %.2f\n", menor, inter, maior);
        system("pause");
        return 0;
    }
    
09.10.2016 / 18:42
0

A different, more efficient solution for your program would be:

void main(){
    int a, b, c, MAIOR, INTER, MENOR;
    scanf("%d %d %d", &a, &b, &c);
    if(a>b){
        MAIOR = a;
        INTER = a;
        MENOR = b;
    } else {
        MAIOR = b;
        INTER = b;
        MENOR = a;
    }
    if((MAIOR > c)&&(MENOR < c)) INTER = c;
    else{
        if(MAIOR < c) MAIOR = c;
        if(MENOR > c){
            INTER = MENOR;
            MENOR = c;
        }
    }
    printf("MAIOR = %d\nINTER = %d\nMENOR = %d\n\n", MAIOR, INTER, MENOR);
}

I do not know if you wanted a program with a different logic, but doing it using several if's is not so practical.

    
10.10.2016 / 04:32
0

A practical solution is to always insert sorted values.

The idea here is the same idea as insertion sort : I keep an ordered part, then add an element at the end and reorder the set based on the new element.

float valores[3];
int i, j;
float maior, inter, menor;

for (i = 0; i < 3; i++) {
  scanf("%f", &valores[i]);
  for (j = i; j > 0; j--) {
    if (valores[i] < valores[i-1]) {
      float swp = valores[i];
      valores[i] = valores[i - 1];
      valores[i - 1] = swp;
    } else {
      break;
    }
  }
}
menor = valores[0];
inter = valores[1];
maior = valores[2];

The last assignments are made and give the correct result because I always keep the valores vector ordered incrementally. This ordering occurs in for that goes in the interval that starts closed in i and ends open in 0 , always changing the element with its previous case detects that they are out of order. Since the vector is always preordered, if one of these checks is not true, then we can close this internal loop because there will be no more substitutions.

    
16.11.2017 / 06:18