Create a program to calculate the hypotenuse of a right triangle. The values for
ID: 3755409 • Letter: C
Question
Create a program to calculate the hypotenuse of a right triangle. The values for the sides of the triangle will be input from the keyboard; the hypotenuse will be calculated using the equation c = (a^2+ b^2 ) where c is the length of the hypotenuse; a and b are the length of the two sides. (c is equal to the square root of the sum of a squared and b squared) (Requires the use of the SQRT() and POW() functions.
Draw the flow chart for the program.
Functions required:
Populate a multidimensional array with random number in the range 3 to 19
Array has 4 rows and 3 columns (see table above)
Calculate the length of the hypotenuse
Values should be saved using 3 decimal place precision
Print the results of all tests of a and b
Print the array holding the results
Use column headings for the output display
All values displayed in 3 decimal-place precision
Program execution:
Define the array in main
Keep in mind the results of the calculations could have decimal places
Populate the array as described above but set the element that will contain the hypotenuse to 0 (zero).
Call the print function
Display the results in table format, including appropriate column headings
Use 3 decimal place precision
Call the calculate function
Pass the side a and b values from the array
Populate the appropriate array element with the value returned for the hypotenuse
Call the print function
Display the results in table format, including appropriate column headings
Use 3 decimal place precision
the flow chart for the program. Side A Side B Hypotenuse (Side C)Explanation / Answer
#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
// Function to display side A, side B and hypotenuse Side C
void print(double data[][3], int row)
{
// Displays the heading
cout<<" Side A Side B hypotenuse(Side C) ";
// Loops till number of rows
for(int r = 0; r < row; r++)
// Displays side A, side B and hypotenuse Side C with 3 decimal places
cout<<fixed<<setprecision(3)<<data[r][0]<<" "<<data[r][1]<<" "<<data[r][2]<<endl;
}// End of function
// Function to calculate hypotenuse Side C
void calculate(double data[][3], int row, int col)
{
// Loops variable
int r, c;
// To store the power sum
double result;
// Loops till number of rows
for(r = 0; r < 4; r++)
{
// Initializes result to zero for each row
result = 0;
// Loops till 2 times for Side a and Side b
for(c = 0; c < 2; c++)
{
// Calculate a^2 + b^2
result += pow(data[r][c], 2);
}// End of inner for loop
// Calculates the square root of a^2 + b^2
data[r][c] = sqrt(result);
}// End of outer for loop
}// End of function
// Function to generate random number for Side A and Side B between 3 and 19
void createData(double data[][3], int row, int col)
{
srand(time(NULL));
// Loops till number of rows
for(int r = 0; r < 4; r++)
// Loops till 2 times for Side a and Side b
for(int c = 0; c < 2; c++)
// Generates random number between 3 and 19 and stores in matrix r and c index position
data[r][c] = (rand() % (19 - 3)) + 3.0;
}// End of function
// main function definition
int main()
{
// Declares a matrix with 4 rows and 3 columns
double data[4][3];
// Calls the function to generate random number and stores in matrix
createData(data, 4, 3);
// Calls the function to calculate hypotenuse Side C
calculate(data, 4, 3);
// Calls the function to display result
print(data, 4);
}// End of function
Sample Output:
Side A Side B hypotenuse(Side C)
12.000 6.000 13.416
10.000 3.000 10.440
5.000 9.000 10.296
5.000 15.000 15.811
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.