acos output does not match correct result

Hello everyone! I have a functioning code but am not getting an output that makes sense to me. I'm sure there's an error here but I am not finding it. In full disclosure this is a homework problem and I am only looking for a little guidance to get the correct result for line 39. The output I find when I run the program is 1.559 degrees for the missing angle B while it should be 48.12 degrees. My instinct tells me I am doing something out of order in this code. Also note, we are not allowed to declare PI and are asked to express pi as acos(-1.0) instead. The example inputs I am using are side a = 20, side b = 15 and angle C = 35.

Thanks!

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
  #include <iostream>
#include <cmath>
#include <iomanip>


using namespace std; //introduces namespace std

int main ()
{
	
	double a, A, B, b, C, c;
	char choice;
	
	do
	{
	
	cout << " This program will find the missing values in a triangle. \n\n Enter the values of side a, side b, and angle C: \n" ;
	cin >> a >> b >> C;   	 
	
	c = sqrt (a * a + b * b - (2 * a * b * cos (C*acos(-1)/180))); 
	B = acos (((pow(a,2) + pow(c,2) - pow(b,2)) / (2 * a * c))*(acos(-1.0)/180)); 	
	A = 180 - B - C;
	
	
	cout << setprecision(4) << " \n The length of side c was found to be " << c << "\n" << endl; ;
  	
 	cout << setprecision(4) << " The angle B was found to be " << B << " \n" << endl;
 		
 	cout << setprecision (4) << " The angle A was found to be " << A << "\n" << endl;
 	
 	cout << " Do you want to calculate for another triangle : y/n ? \n";
 	cin >> choice;
 }
 	while(choice != 'n');
 	
    return 0;
}
Last edited on
Your style of writing everything in one expression makes it hard to track details.
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
#include <iostream>
#include <cmath>
#include <iomanip>

using std::cout;
using std::cin;
using std::setprecision;

int main ()
{
  char choice = 'y';
  do {
    double a, b, C;
    std::cin >> a >> b >> C;
    const auto a2 = a*a;
    const auto b2 = b*b;
    const auto aC = C * acos(-1) / 180; // in radians
    const auto c = sqrt( a2 + b2 - (2 * a * b * cos(aC)) );
    const auto c2 = c*c;
    const auto aB = acos( ( a2 + c2 - b2 ) / (2 * a * c) ); // in radians

    //const auto B = aB ...  // This step is where your actual error is
    //const auto A = 180 - B - C; // in degrees

  } while ( std::cin >> choice && choice != 'n' );
  return 0;
}

keskiverto,

Thanks for taking the time to respond! I am a beginner taking this class two nights a week.

Although we have been studying C++ for almost three months, we are not supposed to submit work without "using namespace std". It must also follow a similar structure to what I presented in the original post in regards to cout and cin (using std :: would not be okay). So while the example you gave is helpful and reading about that structure has taught me new things, I cannot use something like that for my own. I do appreciate your help with this.

Are you able to give any pointers in the code I submit in the original post? I have to stay with a structure like that. Specifically what line(s) I should improve upon? The code runs fine, I am just not getting a value from line 27 that makes logical sense to me.

I'm sure our instructor has a reason for constraining us to only namespace so I have to respect that.

Also, not sure if this is relevant but we are using the DevC++ software.

Thanks

Last edited on
closed account (iN6fizwU)
Read the comment lines in keskiverto's code carefully - he is telling you where the error is.
You are being inconsistent with degrees and radians: that is a maths issue, not a C++ one.
Radio4,

Okay thanks! I'll take a closer look
Last edited on
Topic archived. No new replies allowed.