Incorrect answer generated by program?

#include <iostream>
#include <cmath>

using namespace std;

double MissingSide(double dX, double dY, double dA);

int main() {

double dM, dN, dP;
double dA;
bool bAgain(true);

do {

cout << '\n' << "Please enter the value of the first side: ";
cin >> dM;
cout << "Please enter the value of the second side: "; cin >> dN;
cout << "Please enter the value of the included angle: "; cin >> dP;
double (*call)(double, double, double) = MissingSide;

dA = MissingSide (dM, dN, dP);

cout << "The value of the missing side is: " << dA << endl;
cout << "Would you like to calculate again (y/n)" << endl;
char cAgain;
cin >> cAgain;
cout << '\n';
if (cAgain == 'y') {
bAgain = false;
} else if (cAgain == 'n') {
bAgain = true;
} else {
cout << "Incorrect Response" << endl;
}
} while (!bAgain);
system("pause");
return 0;

}

double MissingSide(double dX, double dY, double dA) {

double d1 = dX * dX;
double d2 = dY * dY;
double d3 = d1 + d2;
double d4 = 2 * dX * dY;
double d5 = cos(dA);
double d6 = d4 * d5;
double d7 = d3 - d6;
double d8 = sqrt(d7);
return d8;
}




*** This program is meant to do the law of cosines for the obtaining of a missing side, not an angle. All the user has to do is enter the values of the two missing sides and the included angle. It runs through the program successfully; however, when I check the answer, i find that it is way off. Help?
For example, if the side values were 7 and 10, and the included angle were 73, the answer should be 10.39; but the program answers 15.88.
Write a program that just calculates cos(x) for different values of x. See if it gives you the answer you are expecting. If not read the manual for cos(x).
Also 1 small thing... Is angles in degree or radians?

http://cplusplus.com/reference/clibrary/cmath/cos/
(radians there)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* cos example */
#include <stdio.h>
#include <math.h>

#define PI 3.14159265

int main ()
{
  double param, result;
  param = 60.0;
  result = cos (param*PI/180);
  printf ("The cosine of %lf degrees is %lf.\n", param, result );
  return 0;
}


Check out row 11
Last edited on
Topic archived. No new replies allowed.