English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

This program prompts the user to enter the lenghts of each of the 3 sides of a triangle. The program outputs the 3 lengths with EQUILATERAL(if all the 3 sides are equal), ISOSCELES(if any 2 sides are equal), SCALENE(if all the sides are different) and NOT a triangle(if the longest side is greater than the sum of the other 2 sides)

I have figured out the if statements for all except for the final case where the program prints"NOT a triangle". Any ideas to the solution of this is most welcome. Assume the variables are x, y and z. for e.g, for the first case where the program prints "EQUILATERAL" ,
the if statement is as follows:
if(x==y&&y==z&&x==z)....

2006-07-16 16:00:14 · 3 answers · asked by jdegbor 1 in Computers & Internet Programming & Design

3 answers

You're first check should be for the not a triangle case, since if it fails there is no point in checking the others.

if( x + y <= z || x + z <= y || y + z <= x )
cout << "not a triangle" << endl;
else if( x == y && y == z )
cout << "equilateral" << endl;
else if( x == y || y == z || x == z )
cout << "isosceles" << endl;
else
cout << "scalene" << endl;

The first check makes sure that we are dealing with a triangle of some sort, otherwise one of the sides will be longer than the sum of the other two.
The second checks if all three sides are equal for an equilateral.
If the second check fails, we just have to check if two of the sizes are equal (isosceles).
If the third check fails, it must be a scalene, since it is a triangle (first check) but not equilateral or isosceles.

2006-07-16 20:07:08 · answer #1 · answered by The Shockwave 3 · 0 0

it's hard to figure out what you're doing with just that small portion of code.... but wouldn't an Else statement work?

I mean if you have the 3 main types of triangles covered, then the only thing left would be "Not A Triangle"..... right?

If
Else If
Else If
Else

2006-07-16 16:05:24 · answer #2 · answered by Anonymous · 0 0

you have one too many && on that equalateral for starters, if x=y and y=z by definition z=x

really to do this, the best way would be to sort the sides entered from shortest to longest.

2006-07-16 16:03:02 · answer #3 · answered by John J 6 · 0 0

fedest.com, questions and answers