Triangle program

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


Just so your aware, you do realize that in this line :

double adj, opp, hyp, a[2], b, c, d, e;

That d and e are not being used right ? If you knew this then no problem. Just giving you a heads up.

As for finding out the Hyp of a triangle, you must know some trigonometry using Algebra and from the Pyth that you mention your on the right path. However I do not see the relevancy of using PI since PI represents a quarter of a circle.

Also, by your picture in ASCII that you show, your representing the A B and C as the corners of the triangle. It should represent the length instead.

So lets say you want to find the length of A and lets say A is the horizontal line's length. You would use B and C to find out what A is.

You would then change your code to use this type of Formula :
b = 3
c = 10
a = sqrt(b^2 - c^2)

The computer can put the answer into a.

What this boils down too, is which one.. A, B, or C that is your Hypotenuse ? Once you know that, then you can adjust your formula based on which one needs an answer.

Mind you I have not written Algebra in years. So I might be a little rusty at it.

Now if you knew all this, then my apologies. But I am just making sure.


Last edited on
Thanks for the swift reply.

d is in the code, just not used at this moment, but thanks for reminding me about e, forgot to remove it.

The ASCII picture: the words are meant to represent the lengths, to reflect the way they are in the code (adj, opp, hyp), A, B & C are for the angles.

I'm using PI (Which is the ratio of any circle's circumference to its diameter, not a quarter of a circle) in this way:
b = asin (opp/hyp) * 180 / PI;

The reason being that b = asin(opp/hyp) gives 'b' in radians, and I use * PI / 180 to convert to degrees.


I know my algebra is correct, I meant is the way that the math is written correct in order for it to be executed in the correct way within the program?


Last edited on
You are correct, now that I am going over some algebra in my head, PI makes perfect sense. Man, has it been that long ? lol

As for the math.. I really hope you find the answer. You have me curious how this could be written. I have not done this in a while. If I do however solve the problem, when I am more awake, then I'll write it back out.

Good luck to you sir, I wish you the best here. I'm very curious about this myself now that you brought it up.
hyp = sqrt (pow (adj,2) + pow (opp,2));

This is only correct for right angled triangles and implies Angle C is 90 degrees.

Also a[1] = acos (opp/hyp) * 180 / PI; or a[1] = asin (adj/hyp) * 180 / PI;

Does this code do what you need?

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
#include <iostream>
#include <cmath>
using namespace std;

#define PI 3.14159265

int main()
{

double adj, opp, hyp, a, 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 = acos (opp/hyp) * 180 / PI;                                    
b = asin (opp/hyp) * 180 / PI;
c = 90.0;
d = a + b + c;

cout << "The Hypotenuse of the triangle is: " << hyp << "\n";


cout << "\n";

cout << "Angle A is: " << a << "\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;
}
Last edited on
Topic archived. No new replies allowed.