) Answer the following (Function Overloading): [1 marks] 1) Write and test a C++
ID: 3568505 • Letter: #
Question
) Answer the following (Function Overloading): [1 marks]
1) Write and test a C++ function (getArea()) that has three double parameters: a, b, and c that
represent the sides of a triangle. Let your function return -1 if the three parameters do not
represent a triangle, and return the area of the triangle otherwise.
Hint: A simple test for that a, b and c represent a triangle is by checking that the sum of any two
numbers is greater than the third.
To calculate the area, use Heron's formula
Explanation / Answer
I give three examples at the end. The output is
I am a beginner in C++.
#include <iostream>
using namespace std;
double getArea(double a,double b,double c)
{ double semip, area;
if ((a+b>c) and (a+c>b) and (b+c>a))
{semip=(a+b+c)/2;
area=sqrt(semip*(semip-a)*(semip-b)*(semip-c));
return area;}
else return -1;
}
double getArea(double a,double b)
{ double area;
area=a*b/2;
return area;
}
double getArea(double a)
{double area;
area=a*a*sqrt(3)/4;
return area;
}
int main()
{double result;
result=getArea(3.1,2.1,5.1);
cout<< "if positive the area is: "<<result<<endl;
result=getArea(2,3);
cout<< "the area of the right triangle is: "<<result<<endl;
result=getArea(6);
cout<< "the area of the equilateral triangle is: "<<result<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.