Journal of computing related issues for Web Authoring, and general topics using Mac, Windows and Linux platforms.

Monday, June 11, 2007

C++ - Hero's Method of Calculating Triangle Area


// tri_over.cpp
// Illustrate overloaded tri_area functions
#include
#include
#include
#include
using namespace std;

double tri_area (double base, double height);
// Base & Height formula
// Area = 1/2 x base x height

double tri_area (double a, double b, double c);
// Hero's Three Sides formula
// Area = Sqareroot [s(s-a)(s-b)(s-c)] where s=a+b+c (i.e. perimeter)

int main()
{
double base, height,
s1, s2, s3,
area;
cout << "Enter base and height of a triangle: ";
cin >> base >> height;
area = tri_area(base,height);
cout << "Area = " << area << endl;
cout << "Enter lengths of sides of a triangle: ";
cin >> s1 >> s2 >> s3;
cout << "Area = " << tri_area(s1, s2, s3) << endl;
system("PAUSE");
return 0;
}

double tri_area (double base, double height)

{double area_bh;
area_bh=(base*height/2.0);
return area_bh;
} // stub

double tri_area (double a, double b, double c)
{ double area_3s, per;
per=((a+b+c)/2);
area_3s=sqrt(per *(per-a)*(per-b)*(per-c));
return area_3s;
}

No comments:

About Me

Husband, dad and grandad. Physics graduate from London University in late 60s. Retired from IBM company in 2000 after 27 years as both Sytems Engineer and Salesman. Interests include photography, nature, science, maths, walking, travel. I like facts as a basis of opinion and not opinion that is assumed to be fact.