-
Identifying Triangles
This program is supposed to let the user input three numbers, rearrange them in increasing order, and identify the properties of the triangle.
For some reason the program works like a beauty, until it hits isRig. It doesn't display anything for isRig's answer. I'm boggled. I looked it over 5 or 6 times, but I'm just starting so I can't find anything. Or maybe im just blind. :cool:
Thank you!
Code:
//Included Files
#include <iostream>
using namespace std;
//Function Prototypes
bool isTri(float, float, float);
bool isIso(float, float, float);
bool isEqu(float, float, float);
bool isAcu(float, float, float);
bool isRig(float, float, float);
bool isObt(float, float, float);
//Main Function
int main() {
//Variable Declarations
float x,y,z;
//Inputs
cout << " Please enter the triangle lengths: "<<endl;
cout << " Length 1: ";
cin >> x;
cout << " Length 2: ";
cin >> y;
cout << " Length 3: ";
cin >> z;
//Answers
cout<<"The triangle is: ";
if (isTri(x,y,z))
{
if (isIso(x,y,z))
{
cout << "Isoceles ";
}
if (isEqu(x,y,z))
{
cout << "Equilateral ";
}
if (isAcu(x,y,z))
{
cout << "Acute ";
}
if (isRig(x,y,z))
{
cout << "Right ";
}
if (isObt(x,y,z))
{
cout << "Obtuse ";
}
}
else
{
cout <<"illegal ";
}
cout<<endl;
return 0; }
//isTriangle
bool isTri(float x,float y,float z)
{
if (x > 0 && ((x + y) > z))
return true;
else
return false;
}
//isIso
bool isIso(float x,float y,float z)
{
if (x == y || y == z)
return true;
else
return false;
}
//isEqu
bool isEqu(float x,float y, float z)
{
if (x == y && y == z)
return true;
else
return false;
}
//isAcu
bool isAcu(float x,float y, float z)
{
if ((x*x)+(y*y)>(z*z))
return true;
else
return false;
}
//isRig
bool isRig(float x,float y, float z)
{
if ((x+x)*(y*y) == (z*z))
return true;
else
return false;
}
//isObt
bool isObt(float x,float y,float z)
{
if ((x+x)*(y*y) < (z*z))
return true;
else
return false;
}
-
First of all, you're not rearranging them to increasing order. (I assume you mean that x should be the smallest, followed by y, and z.)
Second, to check if it is a right triangle, you have
Code:
((x+x)*(y*y) == (z*z))
I think you want
Code:
((x*x)+(y*y) == (z*z))
Edit: OK so bold doesn't work so well with symbols. oops.
Edit2: I apparantly can't type today, but thankfully Thantos is there to pick up the slack.
-
Code:
if ((x+x)*(y*y) == (z*z))
are you sure thats right?
Edit: Beaten by Decrypt so I'll respond to him:
Quote:
I think you want *.
Well he has the right number of * and + but just not in the right order.
-
Sorry, I forgot to say that I hadn't started the increasing order yet and I think you figured it out... lemme check
-
> if ((x+x)*(y*y) == (z*z))
1. go back to school and look up pythagoras
It's if ((x*x)+(y*y) == (z*z))
Your isObt() looks similarly broken
2. Comparing floats for equality doesn't work - just one very tiny error will turn your otherwise apparent equality into inequality.
For floats, you generally need an approximate equality
Code:
bool isApproxEqual ( double x, double y ) {
return fabs(x - y) < 0.000001;
}
Then you say
if ( isApproxEqual( x*x+y*y, z*z) )
-
yes! Ok the blind part was right... Now I need to figure out how to do the increasing order. My teacher tried to explain how to set x as y temporarily??? But it just confused me.
nevermind.. i think i figured it out