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...
// 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'?
usingnamespace 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;
}
#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;
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?
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...