Calculation to determine whether triangle is rectangle gives no expected result

Asked

Viewed 1,045 times

4

The program I created takes three integer values, calculates within the if these values and if the condition is correct it should show on the screen the YES (the triangle is rectangle), and if the condition is not satisfied, it should show NO.

Typing the values 3, 4 and 5 the output has to be YES, typing 3, 3 and 4, the output should be NO, but is giving YES in all cases.

Follows the code:

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

int main (void)

{
    int hip, cat1, cat2;

    scanf("%d", &cat1);

    scanf("%d", &cat2);

    scanf("%d", &hip);

    if (hip = pow(cat1,2) + pow(cat2,2))
    {
        printf("SIM");
    }

    else
    {
        printf("NAO");
    }


    return 0;
}

How to solve?

  • use == instead of =, and also to be the formula of the rectangular triangle must have root.

  • 1

    Turn on your compiler warnings and fix them all before accepting the executable.

  • If your inputs are integer, a simpler solution like those of the bigown response will always bring correct results. Only when one admits floating point inputs (which goes beyond the question, but I am quoting by completeza) is that an approximate solution is necessary - in which case Jjoao’s answer demonstrates the most usual way of dealing with the problem (establish an error margin for FP comparisons). Here it is an example which helps to prove the correctness of the methods presented.

2 answers

8


There are two problems:

  1. You used = and not ==, to compare is the second, you have assigned a new value to the hypotenuse.
  2. You did not use the square root, so the formula is wrong. But there is a better formula.

Behold:

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

int main (void) {
    int hip, cat1, cat2;
    scanf("%d", &cat1);
    scanf("%d", &cat2);
    scanf("%d", &hip);
    if (hip * hip == cat1 * cat1 + cat2 * cat2) printf("SIM");
    else printf("NAO");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference. Note that I did a little different there.

  • 1

    Sugestao: instead of calculating the square root (twice), compare the squares: if (hip*hip == cat1*cat1 + cat2*cat2)

  • @pmg solution is correct. The question is only for integers. Exactly as in http://ideone.com/E3Cxp0. For broken values we use the answer below.

4

Variant of the previous version (@Maniero)

#include <stdio.h>
#include <math.h>
#define ERROMAX 0.0001

int main (void) {
    float hip, cat1, cat2;
    scanf("%f", &cat1);
    scanf("%f", &cat2);
    scanf("%f", &hip);
    if (fabsf(hip - sqrt(pow(cat1,2) + pow(cat2,2))) < ERROMAX ) {
        printf("SIM\n");
    } else {
        printf("NAO\n");
    }
    return 0;
}

so as to give

1 1 1 -> não 

3 4 5 -> sim
  • abs is only for integers (and exists in the stdlib.h, not in the math.h), I think you meant fabs, right?

  • 1

    @mgibsonbr, you have all the reason! I’ll fix it right away! (I was thinking fabsf (for floats) but Fabs (for doubles also gives).

Browser other questions tagged

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