Question: This is C++ code. Write a program called all_area_semi.cpp that computes the area of an arbitrary triangle. Use the following formula: sqrt(s * (s -
This is C++ code.
Write a program called
all_area_semi.cpp that computes the area of an arbitrary
triangle. Use the following formula:
sqrt(s * (s - a)*(s - b)*(s - c))
where a, b and c are the lengths of the sides, and s is the semiperimeter.
s = (a + b + c) / 2
Include a void function call Compute that computes the area and perimeter of a
triangle based on the length of the sides. The function should use five parameters
three call by value parameters (a, b, c) that provide the lengths of the edges and
two cal
l by reference parameters (area, semiperimeter) that store
s
the computed
area and perimeter. Make your function robust. The function
Compute
should
be
called from your main program. Your must print the computed area and
semiperimeter rounded to the nea
rest hundredths.
In main you should declared the
necessary variables
,
and
include
a while
loop asking the user if he/she
would like
to continue computing area and semiperimeters.
Note that not all combinations of a, b, and c produce a triangle.
Remember,
the
sum of any two sides of a triangle must be greater than the third side. You must
make sure that the lengths of the sides of the triangle entered by the user are valid;
if not valid length
s
, you should let the use
r
know by printing a message stating th
at
the lengths
are invalid, and then prompt the user if he/she would like to continue
computing area and semiperimeters
.
Your function should produce correct results
for legal data and reasonable results for illegal combinations.
//include necessary libraries
#include
#include
#include
using namespace std;
//include function prototypes
void Input(int & a, int & b, int & c);
void Output(const int a, const int b, const int c, const double & area, const double & semiperimeter);
void Compute(const int a, const int b, const int c, double & area, double & semiperimeter);
//Function Definitions
void Input(int & a, int & b, int & c)
{
cout << "Please enter the three sides of the triangle: ";
cin >> a >> b >> c;
}
void Compute(const int a, const int b, const int c, double & area, double & semiperimeter)
{
//This function won't calculate the correct areas or perimeters.
double s;
s = (a + b + c) / 2;
area = sqrt(s * (s - a)*(s - b)*(s - c));
semiperimeter = s * 2;
}
void Output(const int a, const int b, const int c, const double & area, const double & semiperimeter)
{
cout << "The Area of the triangle is: " << area << endl;
cout << endl;
cout << "The Perimeter of the triangle is: " << semiperimeter;
}
int main()
{
string user_response = "y";
int a, b, c;
double area, semiperimeter;
cout << "******************************************************************" << endl;
cout << "" << endl;
cout << "~~~ Welcome to the Triangle Area and Perimeter Calculator! ~~~" << endl;
cout << "" << endl;
cout << "******************************************************************" << endl;
cout << "" << endl;
cout << "" << endl;
//while loop
while (user_response == "y" || user_response == "Y")
{
Input(a, b, c);
Compute(a, b, c, area, semiperimeter);
Output(a, b, c, area, semiperimeter);
cout << "" << endl;
cout << "Would you like to do another calculation (Y or N): " << endl;
cin >> user_response;
}
//end while loop
cout << "" << endl;
cout << "Thank you for using the Triangle Calculator!" << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
