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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

// 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 <iostream>
#include <string>
#include <cmath>
#include <stdio.h>
#include <math.h>	// 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
1
2
#define DEGTORAD(D)((D * PI) / 180.0) // Converts Degrees to radians
#define RADTODEG(R)((180.0 * R) / PI)//Converts Radians to Degrees 


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.