Functions + Math

Apr 27, 2017 at 11:15pm
Hi, I am working on a program using the Pythagorean Theorem. As of right now I am stuck with solving the side B and C and then solving for the side A and C. I am to create a function that will solve for any side given two known sides. The Function takes in three parameters, a double for first side, a double for second side and a char to indicate the side to solve. The function should return a double which holds the solved side. I would like to take in some input from you guys or any suggestions that will help me solve my program. Thanks!

Here is my code:

#include <iostream>
#include <cmath>

using namespace std;

double pythag(double, double, char);

int main()
{
double firstSide, secondSide;
char thirdSide = 'C';
double result;

cout << "Pythagorean Theorem, enter side A and B and I will solve for C " << endl;
cin >> firstSide >> secondSide;

result = pythag(firstSide, secondSide, thirdSide);

cout << "Side C is equal to " << result << endl;
cout << "Now enter sides B and C and I will solve for A " << endl;
cin >> secondSide >> thirdSide;

cout << "Side A is equal to " << result << endl;// Where I am stuck at


return 0;
}
double pythag(double sideA, double sideB, char sideC) // Can I solve B and C, and A and C within the function?
{
double calculation;
calculation = sqrt(pow(sideA, 2.0) + pow(sideB, 2.0));
return calculation;
}

Apr 28, 2017 at 12:28am
This is a question from math. Is -lnx + lny = lny - lnx ?
Apr 28, 2017 at 2:01pm
1
2
3
4
cout << "Now enter sides B and C and I will solve for A " << endl;
cin >> secondSide >> thirdSide;

cout << "Side A is equal to " << result << endl;// Where I am stuck at 


In the first case you're given the two shorter sides A and B and need to find the hypotenuse (C).
a*a + b*b = c*c

In the second case, you're given one shorter side B and the hypotenuse C, and need to solve for A. So I'll rearrange the equation to put b and c on the same side.

a*a = c*c - b*b

Left side of the equation is A squared, so to solve for A, take the square root of both sides.

a = square root of c*c - b*b


Apr 28, 2017 at 4:25pm
pow is powerful and can do fractional powers. For that reason it does too much work. Use
X*X for squares.

you can directly return without the intermediate variable.
just say
return sqrt(sideA*sideA + sideB*sideB);

however as noted you need to know which sides you have. You can either take A, B, and C and have the user know that C is the Hypotenuse, or you can have a character that says which, if any, of the inputs it is (say, N for neither, A for A and B for B as an example). There are other ways to handle it as well, but you must do that. You can draw a triangle with sides of 5 and 3 2 ways... one with a side of 4 solved (5 was longest) and one with 5.8etc where the third leg is longer.

May 1, 2017 at 3:34pm
Thanks for all the help! I was able to use the pow function. I ended up making 3 functions for each sides.
Topic archived. No new replies allowed.