The area of an arbitrary triangle can be computed using the formula
where a, b, and c are the lengths of the sides, and s is the semiperimeter.
Write a void function that uses five parameters: three value parameters that provide the lengths of the edges, and two reference parameters that compute the area and perimeter ( not the semiperimeter ). Make your function robust. Note that not all combinations of a, b, and c produce a triangle. Your function should produce correct results for legal data and reasonable results for illegal combinations.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double area,perimeter;
int a,b,c;
void compute(double&,double&,double,double,double);
cout << "Enter the 3 side lengths of the triangle: \n";
cout << " Side 1: "; cin >> a;
cout << " Side 2: "; cin >> b;
cout << " Side 3: "; cin >> c;
compute(area,perimeter,a,b,c);
return 0;
}
void compute (double& area, double& perimeter, double a, double b, double c) {
double s;
if( (a <= 0) || (b <= 0) || (c <= 0) )
{
cout<<"A triangle cannot have a side of zero or less!"<<endl;
return;
}
else if( (a > (b + c)) || (b > (a + c)) || (c > (b + a)) )
{
cout<<"This combination will not make a legal triangle!"<<endl;
return;
}
else
{
s = (a + b + c)/ 2.0;
area = sqrt (s*(s-a)*(s-b)*(s-c));
perimeter = (a + b + c);
cout << "The area of a triangle with side lengths of " << a << ", " << b << " and " << c << " is " << area << " and the perimeter is "<< perimeter << endl;
}
return;
}