(Hypotenuse Calculations) Define a function hypotenuse that calculates the hypotenuse of a right triangle when the other two sides are given. The function should take two double arguments and return the hypotenuse as a double. Use this function in a program to determine the hypotenuse for each of the triangles shown below.
Triangle Side 1 Side 2
1 3.0 4.02 5.0 12.03 8.0 15.0
Solution:
#include < iostream> using std::cout; using std::cin; using std::endl; #include < iomanip> using std::setprecision; using std:: fixed; #include < cmath> double calculateHypotenuse( double, double); double side1; double side2; double hypotenuse; int main() { cout << "Hypotenuse of triangle 1 is " << fixed << setprecision( 2 ) << calculateHypotenuse( 3.0, 4.0 ) << endl << "Hypotenuse of triangle 2 is " << calculateHypotenuse( 5.0, 12.0 ) << endl << "Hypotenuse of triangle 3 is " << calculateHypotenuse( 8.0, 15.0 ) << endl; return 0; } //calculate Hypotenuse function double calculateHypotenuse( double side1, double side2 ) { hypotenuse = sqrt(side1 * side1 + side2 * side2); return hypotenuse; }
Post A Comment:
0 comments: