I recently finished this code. It will ask the user what type of formula it wants to use for calculating the area of the triangle. What I want to know is how I can improve this code and to make it more efficient.
The 3 ways are:
- Heron's Formula (uses sqrt)
- The usage of angle
- The normal way
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int formula;
cout<<"Pick a Formula for computing the area for triangles"<<endl;
cout<<"1. Heron's Formula"<<endl;
cout<<"2. 2 sides and an angle"<<endl;
cout<<"3. Given a base and a Height"<<endl;
cout<<"Formula";
cin>>formula;
switch (formula){
case 1:
int a=0, b=0, c=0, sa=0, s=0, rt=0;
cout<<"Formula is sqrt s(s-a)(s-b)(s-c) where s is (a+b+c)/2"<<endl;
cout<<"Input Parameters"<<endl;
cout<<"a";
cin>>a;
cout<<"b";
cin>>b;
cout<<"c";
cin>>c;
sa=(a+b+c)/2;
s=sa*(sa-a)*(sa-b)*(sa-c);(s);
rt=sqrt(s);
cout<<"The area of the triangle is "<<rt<< "square units";
break;
}
switch (formula){
case 2:
int angle=0, a=0, b=0, zi=0, za=0;
cout<<"Formula is 1/2ab sin (angle)"<<endl;
cout<<"Input Parameters"<<endl;
cout<<"Angle";
cin>>angle;
cout<<"a";
cin>>a;
cout<<"b";
cin>>b;
za=(a*b)/2;
zi=za*(sin(angle));
cout<<"The area of the triangle is "<<zi<<" square units";
break;
}
switch (formula){
case 3:
int ba=0, h=0, area=0;
cout<<"Formula is 1/2bh"<<endl;
cout<<"Input Parameters"<<endl;
cout<<"b";
cin>>ba;
cout<<"h";
cin>>h;
area=ba*h/2;
cout<<"The area of the triangle is "<<area<<" square units";
break;
}
}
There's no problem with it. It works perfectly fine. All I want to know is if it could be made in a way to occupy less space and for it to be more efficient. Anyways, feel free to use this code if you come pass this question.