Innaccurate results with function using cosine law

Hi. I cannot get this computation to result accurately. A push in the right direction is greatly welcome

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  float calculateSideC(float sideA, float sideB, float angleC)
{
const float CONVERSION_VALUE = 3.14159 / 180.0;
float radians;
float sideC;

// Convert the value of angle C to its value in radians using the CONVERSION_VALUE
   radians = (angleC * CONVERSION_VALUE);
   
// Calculate the value of side C using the cosine law.  To square a 
// number, simply multiply the two numbers together rather than call the
// pow() function (e.g., sideA * sideA).  Be sure to use the sqrt()
// function on the math expression before assigning the result to sideC
sideC = sqrt(sideC); 
sideC = sideA * sideA + sideB * sideB - (2 * sideA * sideB * cos (radians)); 
                         
// Return the value of side C
return sideC;

} // End calculateSideC
Last edited on
Be sure to use the sqrt()
// function on the math expression before assigning the result to sideC
this would mean
1
2
sideC = sqrt(sideC); 
sideC = sideA * sideA + sideB * sideB - (2 * sideA * sideB * cos (radians)); 
should either be swapped or simply sqrt before assigning like assignment says so sideC = sqrt(sideA * sideA + sideB * sideB - (2 * sideA * sideB * cos (radians)));

*edit fixed code tags
Last edited on
giblit, I still get a result of 0.00
it should be cos(radians) you have a space which gets the memory address instead of calling the function.
Last edited on
That can't be right. Like, at all. What would "<pointer> (...)" even mean as an expression, if not a function call?
Last edited on
Maybe this will help clarify. I believe the math is correct. Somehow i am not passing the return of sideC to the printResults function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void processRowOfData(void)
{
float sideA;
float sideB;
float angleC;
float sideC;   

// Prompt the user to enter values for side A, side B, and angle C 
   cout << "Enter the values of side A, side B, and angle C (in degrees)." << endl;
   cout << "Separate each value by one or more spaces:" << endl;

// Read in the three values
   cin >> sideA >> sideB >> angleC;

// Call the calculateSideC function, pass the appropriate values to it,
// and store the returned value
   calculateSideC (sideA, sideB, angleC);

// Call the printResults function and pass it the appropriate values
   printResults (sideA, sideB, angleC, sideC); 

} 
Thanks all. I was not calling my function properly which led to inaccurate results.
That can't be right. Like, at all. What would "<pointer> (...)" even mean as an expression, if not a function call?
oh yeah..I don't know what I was thinking I was thinking it was something like cos * (radians) I guess.

The problem is line 17 should be sideC = calculateSideC (sideA, sideB, angleC);
Topic archived. No new replies allowed.