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;
}

C++ Program - Identify Pythagoras Triangle


// Pythagoras-03.cpp

/* Listing of right angle triangles with whole number sides e.g. 3 4 5
Need only try possible values below Squareroot of side_1 sqaured + side_2 squared

I want to treat triangle (3 4 5) as a duplicate of (4 3 5)
Achieve this by only selecting results with side_1 > side_2

Also need to remove multiples such as (6 8 10) (12 16 20)
all multiples of (3 4 5).
Do this by seeing if greatest common denominator > 1 by calling
gcd with side-1 and side_2 to find their gcd and then call gcd
again with this gcd and hyp.
If this is 1 then combination is not a multiple
gcd uses Euclidean method to find gcd.

Virtual printer provides an alternative output source for printer
redirecting to D:\\My Documents\\Temp\\VPRINTER.OUT.
I use this as the console does not keep all of output in lon runs.
You can comment it out if you wish.


*/
#include
#include
#include
#include "D:\My Documents\Borland Studio Projects\mytools.h"
// #include "C:\Documents and Settings\Rod Talboys\My Documents\Borland Studio Projects\mytools.h"

void get_range (int& min, int& max); //out
int gcd (int a, int b); // in-out

void main()

{
// Virtual printer
// This file provides an alternative output source for printer
ofstream vprn ("D:\\My Documents\\Temp\\VPRINTER.OUT");

int max, min, side_1, side_2, hyp, count;
int hypmax, hypmin;

get_range (min, max);
cout << "Pythagorean triangles between " << min << " and " << max << endl;
vprn << "Pythagorean triangles between " << min << " and " << max << endl;

count=0;

for (side_1=min;side_1<=max;side_1++)
{
for (side_2=min;side_2<=max;side_2++)
{
// check if side_1*side_1 + side_2*side_2 is perfect square
// establish hypmin and hypmax
if (side_1>side_2) (hypmin=side_1);
else (hypmin=side_2); // removes surplus
hypmax= floor(sqrt(side_1*side_1 + side_2*side_2)); // processing
for (hyp=hypmin;hyp<=hypmax;hyp++)
{
if ((hyp*hyp==(side_1*side_1 + side_2*side_2))&& (side_1 < side_2))
{
if ((gcd ( hyp, gcd (side_1, side_2))) == 1)
{
cout << side_1 << " " << side_2 << " " << hyp << endl;
vprn << side_1 << " " << side_2 << " " << hyp << endl;
count++ ;
}
}
}
}
}
cout << "There are " << count <<" Pythagorean triangles between " << min << " and " << max << endl;
vprn << "There are " << count <<" Pythagorean triangles between " << min << " and " << max << endl;
system("PAUSE");
return 0;
}

void get_range (int& min, int& max) //out
{
bool valid; // valid for input and prime for prime number

valid = false; // initialise values
max = 0;

while (!valid)
{
cout << "Enter min and max (less than 1,000) values for smaller sides: " << endl;
cin >> min >> max;
if (max > min && max <= 1000)
valid = true;
else
cout << " Invalid entry try again." << endl;
}
}

int gcd (int a, int b)
// Use the Euclidean algorithm to calculate and
// return the greatest common divisor of a and b.
{
int r;
a = labs(a); b=labs(b);
while (b>0)
{ r = a%b; a = b; b = r; }
return a;
}

C++ Programming

I have for years (about 38!) fiddled about with programming. First time was at Rolls Royce in 1968 when bored I played around with a machine about 20x6x4 inches in size programming it to solve quadratic equations.

Then in 1974 I joined IBM as a Systems Engineer (technical marketeer who kept the salesman on this planet - BTW I ended up as a salesman myself) where they taught me to write Assembler. Shame they did not teach me how to get the programs to work. Then had a play around with high level language PL1, good language but did not last. I even taught a class on CICS/VS (don't ask) programming.

So now retired with time to have another play especially as there is a wealth of free compilers, development environments, tutorials and documentation all at the end of broadband. For someone who started writing programs by using coding sheets which were sent to punch room (arghh - nice girls though) and then booking computer time feeding the cards in waiting for the compilation report, correct errors and send back to punch room so the new cards would be ready to do to do the same cycle next day. Today seems like sc-fi has become reality.

I decided to start with C++ and learn enough to be able to code a few sample mathematical exercises because .... strange as it may seem .... I wanted too.

First to select a reasonable, free, compiler that preferably work on Linux too, my longer term target environment. I looked at both Borland Turbo C++ and MS Visual C++, both worked but environment too sophisticated for my immediate needs as I want to be able to understand what is going on.

So I am using Bloodshed Dev-C++ which you can find at http://www.bloodshed.net/devcpp.html
and so far this has been fine.

To get me started I am reading "Using C++" by Julien Hennefeld and CharlesBurchard. It assumes you know little and approaches C++ from a traditional background of code examples and does not introduce Objects until mid way through book. It is published by PWS and its web pages seem to have disappeared so at first I could not get code examples but I found a site St Edwards who have put all the code up on their web pages at http://myweb.stedwards.edu/laurab/bookcode/

For online C++ Reference I currently use

I will put my code samples on the blog. They are of no consequence and put there mainly as backup and documentation for myself.

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.