Need help with C++ and right triangles

MaxDSP

Lifer
May 15, 2001
10,056
0
71
This is what the program is supposed to do:

2.36: You must test for all the possible orientations of the input values as possible sides.
For example, if x, y, z are the input variables and
in an imaginary tryiangle a is the height, b the base, and c the hypotenuese, then you must check for
x = a b c
y = b c a
z = c a b

This is what I have for the core of the program:
{

int a, b, c, a2, b2, c2;

cout<<"Enter three numbers to see if they are the sides of a right triangle: \n";
cout<<"\n";
cin>>a2;
cin>>b2;
cin>>c2;

if (c2=sqrt((a2*a2)+(b2*b2)))
{
cout<<"These numbers CAN form a right triangle";

}

else if (b2=sqrt((a2*a2)+(c2*c2)))
{
cout<<"These numbers CAN form a right triangle";

}


else if (a2=sqrt((c2*c2)+(b2*b2)))
{
cout<<"The number CAN form a right triangle\n\n";

}

else
{
cout<<"The numbers CANNOT for a right triangle";

}


return (0);


}


How do I take the square root of something, thats the problem. see any others?

 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
isn't there a square root function in math.h or something?

an easy work around is just to square a2, b2, and c2... negating the need for the square root.
 

Tdawg951

Member
Nov 28, 2001
169
0
0
gopunk is right

there is a function in math.h called sqrt() but it would probably be easier just to square both sides and see if they are equal
 

MaxDSP

Lifer
May 15, 2001
10,056
0
71
isnt that what im doing though?

if (c2=sqrt((a2*a2)+(b2*b2)))
{
cout<<"These numbers CAN form a right triangle";

}

I am using the sqrt() function but only on the right side. If I took the square root of the left side, its never going to a right triangle.

For example, 3-4-5 is the most common set used. You have (3*3)+(4*4), so thats 9+16. You take the square root of 25 for the left side and you get 5. There wouldnt be a need for the right side to have the square root, since its already done on the other side.

Or am I confused?
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
I am using the sqrt() function but only on the right side. If I took the square root of the left side, its never going to a right triangle.

that's correct... btw i just noticed you're using = and not ==

Or am I confused?

i was just saying you could take the input, square it, and use that as the value in the comparison.
 

crypticlogin

Diamond Member
Feb 6, 2001
4,047
0
0
Equality comparing a root of another arithmetic function (in this case, the sum or squares), on floats no less, is a dangerous test. I think in this case, you would be better off doing a comparsion of squares, not of a square root.

5 != 4.9999994

edit: and gopunk's on the money with '=' vs. '=='. Can't believe I missed that. :eek:
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
okay how the f*ck do you make a simple C++ console app in VS.NET? i always end up with some weird files in my project..
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
Here's an easy way to catch the '=' vs '==' in a conditional. Put the one that CANNOT be changed by a '=' on the left.

if (c2=sqrt((a2*a2)+(b2*b2)))

Notice that this line was syntactically correct but the meaning was wrong.

Switching them around will give this:

if (sqrt((a2*a2)+(b2*b2))=c2)

This line won't compile, and it's a little easier to see that the error will be using '=' instead of '=='. Hope this helps.

By the way, it may be easier to test to see if squaring the third side is equal to the sum of the squares of both sides as opposed to testing for the square root. You won't have the rounding error with the square root function.

[edit]
Oops, gopunk already suggested this. :eek:
[/edit]
 

MaxDSP

Lifer
May 15, 2001
10,056
0
71
WOOOOOOHOOOOO!!! I think I got it. I tested 3-4-5, and it returned a positive. I tested 5-5-5 and it returned a "Cannot". seems to be in working order. Can anyone think of any other sets like 3-4-5 that I can test the program further with?

BTW, heres the final source code. Unless anyone spots any trouble, Im gonna start putting comments in and work on the next project. Thanks everyone :D

#include <iostream>
#include <math.h>
using std::cout;
using std::cin;
using std::endl;
int main(void)
{
double a, b, c, a2, b2, c2;
cout<<"Enter three numbers to see if they are the sides of a right triangle: \n";
cout<<"\n";
cin>>a;
cin>>b;
cin>>c;
a2=(a*a);
b2=(b*b);
c2=(c*c);
if ((c2)==(a2+b2))
{
cout<<"These numbers CAN form a right triangle\n\n";

}
else if ((b2)==(a2+c2))
{
cout<<"These numbers CAN form a right triangle\n\n";

}
else if ((a2)==(c2+b2))
{
cout<<"These number CAN form a right triangle\n\n";

}
else
{
cout<<"These numbers CANNOT form a right triangle\n\n";

}

return (0);
}


 

Aves

Lifer
Feb 7, 2001
12,233
31
101


<< okay how the f*ck do you make a simple C++ console app in VS.NET? i always end up with some weird files in my project.. >>


I've been wondering the same thing for a while.
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
5-12-13

i'm sure you can find a more complete list somewhere...

btw, do you have to do error checking? i suspect not, but i just wanted to make sure...
 

eLiu

Diamond Member
Jun 4, 2001
6,407
1
0
Well, taking 3, 4, 5 and doubling it, tripling, etc will continue to generate Pythag triples.

For new sets:

in the form of A^2 + B^2 = C^2...the general solution is this:
A=2xy
B=x^2 - y^2
C=x^2 + y^2

where x and y are relatively prime integers, with 'opposite parity', and x>y>0

so...if u make urself a little table thing, and start plugging in values, you'll start generating pythag triples...a few to get you started:
8 15 17
12 35 37
16 63 65
20 99 101
12 5 13
20 21 29
28 45 51
24 7 25

Perhaps you could write a program to generate triples...which could be saved to a file and accessed by the checker? (Tho methinks that's too much trouble lol)
 

Yomicron

Golden Member
Mar 5, 2002
1,735
1
81


<<

<< okay how the f*ck do you make a simple C++ console app in VS.NET? i always end up with some weird files in my project.. >>


I've been wondering the same thing for a while.
>>




I was wondering this too, but I just figured it out.


-create a new project
-select Visual C++ Projects -> win32 Project
-click OK
-click application settings
-select console app
-(EDIT) oh ya, select blank project
-click OK
-right click source files and select new
-name and save your file
-write the code
-click the build button
-click the start button
-hope I didn't miss a step
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
yomicron - thanks... yea i figured out what stdafx.cpp was :eek:

does anybody else have a problem with iostream though? i'm trying to include it so i can use cout and cin, but when i include iostream.h, it tell me to use <iostreams>... but when i do that, it tells me it can't find the file. wtf.

i normally like ms stuff, but VS.net is starting to piss me off.
 

MaxDSP

Lifer
May 15, 2001
10,056
0
71
GoPunk, no, we dont HAVE to do error-checking, but I may put some if-else statements to tell the user if he/she enters a non-numerical value and to enter the numbers again. At least it wouldnt cause an infinte loop with Visual C++ 6 as it did back in high school with Borland C++

BTW, thanks aves2k, Ill use those to test it out later

:)