Problem with second degree equation program

Asked

Viewed 132 times

1

I have an interesting problem in my code, the program reads three numbers and says whether it’s a rectangle triangle or not, but the problem is that depending on the sequence I type in the numbers, the program of the error, because the highest value entered must be that of the hypotenuse and whatever the other two catects, if I type 3, 4, 5, the value of 5 must be stored in the variable hypotenuse, but how do I do it, 5, 4, 3 of the right but the opposite not...

#include <stdio.h> //Inclusao da bibilioteca principal
#include <math.h> //Inclusao da biblioteca para uso da funcao "pow" e "sqrt"

int main (void) //Declaracao do corpo principal do programa

{
    int hip, cat1, cat2; //Declaracao de variaveis hipoteusa, cateto 1 e cateto 2

    scanf("%d", &hip); //Insercao pdo valor da hipotenusa
    scanf("%d", &cat1); //Insercao do valor do cateto 1
    scanf("%d", &cat2); //Insercao do valor do cateto 2

    if (hip == sqrt(pow(cat1,2) + pow(cat2,2))) //Condicao para caso o valor da hipotenusa seja igual a raiz quadrada da soma dos quadrados dos catetos
    {
        printf("SIM"); //Caso a condicao acima seja verdade, imprimi na tela a palavra SIM
    } else {
        printf("NAO"); //Caso a condicao acima nao for satisfeita, imprimi na tela a palavra NAO
    }

    return 0;
}
  • 2

    Hypotenuse is always the largest side of the triangle. You can read side1, side2, side3, in any order. Then you do a function to find out which side is bigger. And only then do you apply your formula.

2 answers

2


The problem is in logic. You assumed that the user will first insert the hypotenuse, and then the catheters. The largest number inserted should be checked and stored in the hip variable. Here’s an example:

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

int main(){
    int hip, cat1, cat2, n1, n2, n3;
    scanf("%i", &n1);
    scanf("%i", &n2);
    scanf("%i", &n3);
    // Verifica se a hipotenusa é o n1
    if(n1>n2 && n1>n3) {
        hip = n1;
        cat1 = n2;
        cat2 = n3;
    } else if(n2>n1 && n2>n3) { //Verifica se a hipotenusa é n2
        hip = n2;
        cat1 = n1;
        cat2 = n3;
    } else {
        hip = n3;
        cat1 = n1;
        cat2 = n2;  
    }
    if(hip == (pow(cat1, 2) + pow(cat2, 2))) {
        printf("Yes\n");
    } else {
        printf("No\n");
    }
    system("pause");
}

I tested it here worked. See if that’s your goal. At. Samuel Gomes

2

Follow an alternative to @Samuelgomes code, with the rounding problem solved with the removal of the sqrt:

#include <stdio.h>

int main (void)

{
    int n1, n2, n3, t;

    scanf("%d", &n1);
    scanf("%d", &n2);
    scanf("%d", &n3);

    if ( n2 > n1 ) {
        t  = n1;
        n1 = n2;
        n2 = t;
    }

    if ( n3 > n1 ) {
        t  = n1;
        n1 = n3;
        n3 = t;
    }

    if ( n1 * n1 == n2 * n2 + n3 * n3 )
    {
        printf("SIM");
    } else {
        printf("NAO");
    }

    return 0;
}

The answers to the previous question deal with the rounding problem:
Calculation to determine whether triangle is rectangle gives no expected result

Browser other questions tagged

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