### C++ Calculating a triangle's angles given side lengths.

Hi,

I'm working on my first C++ project for my college CPA course. So far we've only had a small handful of classes, and I'm stuck.

The program requires the user to enter 2 side lengths for a right angle triangle. From there we have to calculate the hypotenuse and the remaining 2 angles of the triangle.

So far I have user input being taken and the hypotenuse being calculated. The part I'm stuck on is calculating the remaining 2 angles. I've refreshed my memory on how to do this on paper like I did back in high-school math class, but I can't seem to program it if my life depended on it. So far I've spent 2 hours trying to figure this out and I've gone to at least a dozen websites to try to find code that I can cannibalize, but I haven't had any luck...

Can someone help me out please?

Here's what I have so far:

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 // Project 0 - Triangle // Calculates the hypotenuse and angles of a right triangle. // This program prompts the user for 2 values (mm, cm, m, km) -- Units of measure will be added later. #include #include #include #include #include // do I need all of these #includes'? using namespace std; #define PI 3.14159265 int main() { double xLength; double yLength; double zLength; double aAngle; double bAngle; double cAngle = 90; // Draw a pretty picture (helps the user to better understand what they are entering) cout << " |* "; cout << "\n | * "; cout << "\n | B * Z"; cout << "\n | * "; cout << "\nX | * "; cout << "\n | * "; cout << "\n | 90 A * "; cout << "\n |_____________________* "; cout << "\n Y \n\n"; // Prompt the user. std::cout << "Enter the length of the two sides adjacent to the right angle. Include units of measure. (mm, cm, m, km)" << endl; // Units of measure will be added later. // Get side lengths // Get the Length of "x". cout << "x = "; cin >> xLength; cout << "You entered " << xLength << "." << endl; // Get the Length of "y". cout << "y = "; cin >> yLength; cout << "You entered " << yLength << "." << endl; // Calculate the Hypotenuse zLength = sqrt( xLength * xLength + yLength*yLength ); cout << "Hypotenuse = " << zLength << endl; // Calculate angles // Calculate Anlgle 'a' // cout << "The angle of angle 'a' is: " << aAngle << endl; // Calculate angle 'b' return 0; }

Thanks in advance to anyone who can help. I really appreciate it!
Last edited on
You can try this for getting degree output

Below should get the angles, I used your already labled sides and angles of ABC XYZ, the lowercase xyz was added by me, I took this out of a triangle calculator I made a long time ago so it should work

 123456789101112131415 double x,y,z; //Angle A y = X / Z; x = asin(y); z = RADTODEG (x); A = z; //Angle B y = Y / Z; x = asin(y); z = RADTODEG (x); B = z;
Last edited on
Thanks, but how would I calculate what angle 'A' and angle 'B' are?
cmath includes the functions sin() and cos(), which you'll be needing for this program.. How's your trigonometry skills? Do ya know how to do the math?
Only thing is, cos() and sin() return angles in radians. You'll want to use something like Barda suggests ^ to convert the angle to degrees.
I can kinda do trig on paper like what was learned in high-school math class, however it's been a few years and I can't seem to figure out how to code it. I did mess around with the sin() and cos() in my struggles, however I had no luck. All the numbers I got were negative or way to high to be correct...
yeah check out barda's edit ^
function you need is arc sine: asin();
if you know your trigonometric ratios, just plug in the sides you need

e.g.

float angle_a, opposite, hypoteneuse;
angle_a = asin(opposite / hypoteneuse);

once again, this will return radians not degrees.. you do the math :p
Last edited on
Thanks guys, that worked great!
Topic archived. No new replies allowed.