Public Group

# Help with program. Triangle Solver. Half-Way there. Debug

This topic is 4885 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

/* This program finds the remaining unknowns in a triangle with 2 sides known, A and B.
The angle inbetween, c, is also known. */

#include <iostream>
#include <cmath>
using namespace std;

int main()
{

double side_A;
double side_B;
double angle_c;

cout << "Please enter two sides of the triangle" << endl;
cin >> side_A;
cin >> side_B;

cout << "Please enter the angle inbetween those sides" << endl;
cin >> angle_c;

double side_C;

//Find side_C using Law of Cosines.

side_C = ((side_A * side_A + side_B * side_B) - (2*side_A * side_B* cos(angle_radians)));
side_C = sqrt(side_C);

double angle_a;
double angle_b;

// Now I find Angle A using Law of Sines....Side A/Sine A= Side B/Sine B= Side C/Sine C
// The result of the law of sines is the sin of angle A. I must then find the inverse.(asin)

PROGRAM IS MESSING UP HERE

angle_a = (side_A * sin(angle_c))   / side_C;

PROGRAM IS MESSING UP HERE

angle_a = asin(angle_a);

PROGRAM MESSING UP HERE ^^^^^^^^^^^^

// Now I find angle_b by subtracting two known angles from 180.

angle_b = (180 - (angle_a + angle_c));

cout << "The angle in radians is: " << angle_radians << endl;

cout << "The third side is: " << side_C << endl;
cout << "The two remaining angles are: " << angle_a << " and " << angle_b << endl;

return 0;

}


The program works perfectly on paper, using the same exact steps. Side C is produced perfectly. Angle_a is messed up, and angle_b is too, because angle_b is found using angle_a. Anyone? Thx [Edited by - ncasebee on January 4, 2005 11:03:04 PM]

##### Share on other sites
Maybe try
angle_a = (side_A * sin(angle_radians))   / side_C;instead ofangle_a = (side_A * sin(angle_c))   / side_C;

##### Share on other sites

I see now. Your definately on to something. I solved all my others in radians, I have to use it here too. I'm still not getting the right answer, but I'm gonna fiddle more with radians.

Ok I'm getting the right sin A = .5447. Both checking out ok.

So up to taking the inverse of angle_a to find the actual angle, not the sin A, is working fine. How using asin(angle_a) is messing it up.

When I take the asin(angle_a), it doesn't work. What does asin need? How do I use it?

I'll update code now to point to exactly where the problem is occuring.

##### Share on other sites
Yeah, I didn't actually look at the math, but the standard library math functions use radians, so don't mix them.
Meanwhile, make the program output the result of each step, feed it some values you know, and see which step has the problem.

angle_b = (180 - (angle_a + angle_c));

Change that too.

##### Share on other sites
put

angle_a *= 180/3.141592653589793;

after

angle_a = asin(angle_a);

##### Share on other sites
After woops, put it before.

What is that doing? I've never seen that notation. (*)?

WORKED.

But what the heck is that?

angle_a *= 180/3.141592653589793;???

What is (*)? where is angle_a after the =?

asin()....Ahh it takes the sine value of an angle, and spits out the angle in radians. Then you must convert the angle to degrees. AHHHHH. Very tricky.

Could you explain that notation though? (*)

Also, I wrote this program all by my lonesome. Problem P2.7 Page 74 in BIG C++. Pretty good eh? Long way to go, yes I know.

##### Share on other sites
Quote:
 Original post by ncasebeeCould you explain that notation though? (*)

a*=some_number;
means
a=a*some_number;

##### Share on other sites
Thx a ton. Case closed

[Edited by - ncasebee on January 4, 2005 11:15:46 PM]

• 17
• 10
• 19
• 14
• 19