(Numerical) Heron\'s formula for the area, A, of a triangle with sides of length
ID: 3639726 • Letter: #
Question
(Numerical) Heron's formula for the area, A, of a triangle with sides of length a,b, and c is
A=v[s(s-a)(s-b)(s-c)]
where
S=(a+b+c)/2
Write test and execute a function that accepts the values of a, b, and c as parameters from a calling function, and then calculate the values of s and [s(s-a)(s-b)(s-c)]. If this quantity is positive the function calculates A and if the quantity is negative a, b, and c do not form a triangle, and the function should set A=-1. The value of A should be returned by the function.
Explanation / Answer
int TriArea(int a, int b, int c)
{
int area2,area;
area2 = ((x+y+z)*(x+y-z)*(y+z-x)*(z+x-y))/16;
if (area2<0)
{ area = -1;}
else
{area=sqrt(area2);}
return area;
}
int main()
{
int x, y, z, area;
cout << "Please enter the side lengths:" << endl << endl;
cout << "Side 1: ";
cin >> a;
cout << endl;
cout << "Side 2: ";
cin >> b;
cout << endl;
cout << "Side 3: ";
cin >> c;
cout << endl;
area = TriArea(a, b, c); // use the function
cout <<"area:"<< area<< endl;
return 0;
}
#include <iostream.h> Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.