• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need help with C++ and right triangles

MaxDSP

Lifer
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?

 
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.
 
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
 
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?
 
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.
 
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. 😱
 
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..
 
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. 😱
[/edit]
 
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 😀

#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);
}


 


<< 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.
 
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...
 
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)
 


<<

<< 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
 
yomicron - thanks... yea i figured out what stdafx.cpp was 😱

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.
 
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

🙂
 
Back
Top