Jump to content
  • Advertisement
Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 angle_radians = (angle_c)*(3.141592653589793/180);


	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)


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


	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 this post

Link to post
Share on other sites
Maybe try

angle_a = (side_A * sin(angle_radians)) / side_C;
instead of
angle_a = (side_A * sin(angle_c)) / side_C;

Share this post

Link to post
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 this post

Link to post
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 this post

Link to post
Share on other sites
After woops, put it before.

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


But what the heck is that?

angle_a *= 180/3.141592653589793;???

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

180/3.14 tells me your changing something from radians to degrees.

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 this post

Link to post
Share on other sites
Original post by ncasebee
Could you explain that notation though? (*)


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!