Program in C : A certain doubt

Asked

Viewed 178 times

2

The program I made compiles, but what is requested does not appear on the screen. That’s the thing about:

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;
}
  • 3

    "What is asked" is to enter the values, that does not appear on the screen? Or the results? Something else, why in C if the statement loses Portugol?

  • The results do not appear and I did in portugol and ran in visualg, but when I passed to C no result appeared. Appears what is requested in the printf, but not the results.

  • 2

    It is that you define the values of the variables, but at no time print! Search on the command printf of the C.

  • I get it. Thank you.

3 answers

2

Another very efficient solution is to use For loops with selection structures applied inside the loop, which also allows the code to be smaller and more readable. At the end of the code I used the function printf(), 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;
    }
  • 1

    There are errors in your code.. It doesn’t work for numbers inserted in ascending order, for example

  • 1

    Hello, William! I made small changes to the code to solve this problem.

0

A different and 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 don’t know if you wanted a program with a different logic, but, do using several if’s is not so practical.

0

A practical solution is to always insert ordered values.

The idea here is the same idea as insertion sort: i keep an orderly 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 vector valores increasingly ordered. This ordering takes place in the for that goes into the interval that starts closed in i and ends open in 0, always trading the element with your previous case detects that they are out of order. Since the vector is always previously ordered, if one of these checks is not true, then we can close this internal loop because there will be no more substitutions.

Browser other questions tagged

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