Problem with program of equation of the second degree

1

I have an interesting problem in my code, the program reads three numbers and says whether it is a triangle rectangle or not, but the problem is that depending on the sequence I type the numbers, program the error because the higher value typed must be that of the hypotenuse and both the other two legs, if I enter 3, 4, 5, the value of 5 needs to be stored in the variable hypotenuse, but how do I, 5, 4, 3 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;
}
    
asked by anonymous 19.04.2015 / 03:00

2 answers

2

The problem is in logic. You assumed that the user will first insert the hypotenuse, and then the cathets. It must be verified which the largest number is inserted and store it in the variable hip. 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");
}

Tested here worked. See if that's your goal. At, Samuel Gomes

    
19.04.2015 / 03:20
2

Here is an alternative to the @SamuelGomes code, with the problem of rounding solved by removing 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 if triangle is rectangle does not give expected result

    
19.04.2015 / 06:45