# right angle triangle help

• 10-08-2010
jackalope
right angle triangle help
I need to make a program which calculates whether or not a triangle is a right angle triangle based on the input of the 3 sides. heres my code wondering if anyone can point out the errors?
Code:

```/* This program will calculate wether or not the values entered by a user consitute a right angle triangle*/ #include <stdio.h> #include <stdlib.h> #include <math.h> int main() {         float a, b, c;                 int sum;         printf("please enter your values of length for the sides of your triangle.\n a and b will be the shorter sides while the value you enter for \nc must be the greatest length\n");                 printf(" enter a:\n");                         scanf("%f", &a);                 printf(" enter b:\n");                         scanf("%f", &b);                 printf(" enter c:\n");                         scanf("%d", &c); sum = sqrt(a*a+b*b);         if (sum == c)         {                 printf("your values indicate the triangle is a right angle triangle\n");         }         else         {                 printf("The values entered do not indicate a right angle triangle\n");         }         system ("pause");         return 0; }```
• 10-08-2010
ಠ_ಠ
sum is an integer, you are comparing it to a float, unless sqrt(a*a + b*b) is a whole number, it won't be able to store the proper result
• 10-08-2010
iMalc
Quote:

Originally Posted by jackalope
I need to make a program which calculates whether or not a triangle is a right angle triangle based on the input of the 3 sides. heres my code wondering if anyone can point out the errors?

If by that you mean, can we do a code review:
• You've spelt "whether" incorrectly.
• system("pause") is not the recommended approach.
• 10-08-2010
Code tags: √
good brace style: √
accuracy: +-

I'd like to see it done very "cleanly", using the classic equation: A²+B² = C².
And leave out the sqrt() function, entirely.

Maybe:
if((C*C) == ((A*A)+(B*B)))

and leave out the sum variable. All variables can be unsigned long if you need that high a range. Because inches to feet is base 12, instead of 10, (not to mention problems with yards, etc.), I'd be very temped to FIRST change every measurement to it's *metric* equivalent distance. Now all your base 10 arithmetic works fine.

If a measurement is say, 10 ft. 4 inches. I'd first change that into all inches, and then use 2.54 cm to the inch, to convert it to metric. Then convert all the other measurements to cm's, (centimeters), also.

Now there's no problem comparing floats or doubles (which is a bit of a headache).

In your comments - good essay style, but it's a program note, not an essay. Keep them short, and to the point, and don't mention the obvious. It's the
"unusual" code that needs the comments. (Your teacher may differ on this idea, however). :p
• 10-08-2010
jackalope
thanks everyone comments were very helpful especially you adak. I made the necessary changes but im still left with the problem that it always returns that the triangles is not a right angle triangle. I am guessing its because the program wont round anything. Is there a way to make it accept only 2 significant figures? o and about the verbose description it's what was in the examples i was given by the prof so i figured I'd play it safe :)
• 10-08-2010
Dino
Print out the difference between sum and c, and then give yourself a fudge factor and check for the difference being in the range of your fudge factor.
• 10-08-2010
jackalope
I changed it to this. May not be exact but its accurate to within 0.1, so not too shabby.

Code:

```/* This program will calculate whether or not the values entered by a user consitute a right angle triangle*/ #include <stdio.h> #include <stdlib.h> #include <math.h> int main() {         float a, b, c;                 float sum, difference;         printf("please enter your values of length for the sides of your triangle.\n a and b will be the shorter sides while the value you enter for \nc must be the greatest length\n");                 printf(" enter a:\n");                         scanf("%f", &a);                 printf(" enter b:\n");                         scanf("%f", &b);                 printf(" enter c:\n");                         scanf("%f", &c);         sum = sqrt((b*b)+(a*a));         difference = c - sum;         if (difference <= 0.1 )         {                 printf("your values indicate the triangle is a right angle triangle\n");         }         else         {                 printf("The values entered do not indicate a right angle triangle\n");         }         system ("pause");         return 0; }```
• 10-08-2010
Dino
Just FYI, this fudge factor's proper name is Epsilon. Machine epsilon - Wikipedia, the free encyclopedia