Hello all, first off I'm new here and am quite inexperienced when it comes to code, but so far I am enjoying learning from a book I have and the tutorials on this website, but I spend most of my time tinkering about and googling my way through, and alot of my information comes from this website, so thank you!
I started a program to calculate the hypotenuse of a triangle using pythagorean theorem, and it worked perfectly, however now, I want it to be an all in one Triangle tool, for calculating sides and angles...and angles are killing me.
I'll paste in the full code and specify where I'm having trouble, but if any of you more experienced/knowledgeable see a place I can improve/change things, please let me know.
*NOTE* There are some lines that are commented, they were made that way so I could compile the program without removing them entirely, as they are not functioning at the moment.
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <iostream>
#include <cmath>
using namespace std;
#define PI 3.14159265
int main()
{
double adj, opp, hyp, a[2], b, c, d, e;
char f;
cout << "*********************************************\n";
cout << "* Placeholder *\n";
cout << "*********************************************\n";
cout << "* Placeholder *\n";
cout << "*********************************************\n";
cout << "* *\n";
cout << "* * *\n";
cout << "* * * *\n";
cout << "* * B * *\n";
cout << "* * * A *\n";
cout << "* * * d *\n";
cout << "* Hypotenuse * * j *\n";
cout << "* * * a *\n";
cout << "* * * c *\n";
cout << "* * * e *\n";
cout << "* * * n *\n";
cout << "* * * t *\n";
cout << "* * A C * *\n";
cout << "* ************************ *\n";
cout << "* Opposite *\n";
cout << "* *\n";
cout << "*********************************************\n";
do
{
loop1:
cout << "\n";
cout << "Enter the value of the adjacent: ";
cin >> adj;
if (adj <= 0) goto loop1;
loop2:
cout << "\n";
cout << "Enter the value of the opposite: ";
cin >> opp;
if (opp <= 0) goto loop2;
cout << "\n";
if (adj > 0 && opp > 0)
{
hyp = sqrt (pow (adj,2) + pow (opp,2));
a[0] = pow (hyp,2) + pow (opp,2) - pow (adj,2) / (2 * opp * hyp);
a[1] = acos (adj/hyp) * 180 / PI;
b = asin (opp/hyp) * 180 / PI;
c = atan (opp/adj) * 180 / PI;
//d = a + b + c;
cout << "The Hypotenuse of the triangle is: " << hyp << "\n";
cout << "\n";
if ( adj == opp)
{
cout << "Angle A is: " << a[1] << "\n";
}
if (adj != opp)
{
cout << "Angle A is: " << a[0] << "\n";
}
cout << "Angle B is: " << b << "\n";
cout << "Angle C is: " << c << "\n";
//cout << "Total of Angles: " << d << "\n";
}
cout << "Do you want to calculate another? (Y/N): ";
cin >> f;
} while (f == 'y' || f == 'Y');
return 0;
}
|
The array a on lines 62 & 63 are giving me trouble, a[0] only works on non-right triangles and a[1] only works on right-triangles, is this the correct way to do this?
For the calculations between 62-65, is the way I've written the math correct? I am obviously getting results all over the place, such as currently, if you input adj & opp both as 3, the result shows a, b & c all being 45, when the angle c should infact be 90 in that situation.
Also, on line 66, the value of d will obviously be different depending on which of a[2] is taken, is there any way to specify this at the same time as lines 73 & 77?
When I have the basic code down so that I can have the angles displayed after every calculation, I'd like to expand the program to be able to calculate the opposite & adjacent sides too, is there any way I can create an 'option' at the start, for the user to choose which they want calculated?
Again, any advice I can get aswell on any part of this would be very nice as I am trying not learn any bad habbits as I go along. And if you need me to explain why I've done something, or need more information to help, please just ask.
Regards
G