7. (Numerical) Heron\'s formula for the area, A, of a triangle with sides of len
ID: 3891066 • Letter: 7
Question
7. (Numerical) Heron's formula for the area, A, of a triangle with sides of length a, b, and c is A-Vss a)(s - b)(s -c) where (a + b + c) Write, test, and execute a function that accepts the values of a, b, and c as parameters from a calling function, and then calculates the values of s and s(s - a)s bs c)]. If this quantity is positive, the function calculates A. 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
Hi, here is the fully executable code, i have added comments to help you understand,
#include <iostream>
#include<math.h>
using namespace std;
float area(int a,int b,int c)
{
float s=(a+b+c)/2; //perimeter
float x=s*(s-a)*(s-b)*(s-c);
if(x<0)
return -1; //if not valid sides
else
return pow(x,0.5); //return sqrt
}
int main()
{
cout<<area(6,1,4)<<endl; //testing for various data
cout<<area(3,4,5);
return 0;
}
Thumbs up if this was helpful, otherwise let me know in comments
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.