The area of a triangle can be calculated give n its base and height, or given th
ID: 3759780 • Letter: T
Question
The area of a triangle can be calculated given its base and height, or given the lengths of its sides. You can find the formulas for each of these methods on the web. Write a program with two overloaded functions both named triangleArea. In main, randomly ask the user to enter either base and height or the lengths of the three sides and then make the appropriate call to triangleArea. triangleArea should return the area to main where it will be displayed. The program should contain an outer loop which asks if the user wishes to continue. A typical run would be:
Explanation / Answer
#include <iostream>
#include<math.h>
using namespace std;
float triangleArea(float base, float height) {
return .5*base*height;
}
float triangleArea(float a, float b, float c) {
float p = (a+b+c)/2.0;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
int main() {
while (true) {
cout<<"Enter 1 to enter sides 2 to enter base and height and anything else to exit Enter: ";
int ch;
cin>>ch;
switch (ch) {
case 1:
float a,b,c;
cout<<"Enter 3 sides seperated by space: ";
cin>>a>>b>>c;
cout<<"The area is: "<<triangleArea(a, b, c);
break;
case 2:
float base,height;
cout<<"Enter base: ";
cin>>base;
cout<<"Enter height: ";
cin>>height;
cout<<"The area is: "<<triangleArea(base, height);
break;
default:
cout<<"Good Bye!!";
return 0;
break;
}
cout<<" ";
}
cout<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.