So here is my question:
The area of an arbitrary triangle can be computed using the formula
Area = sqrt( s * (s-a) * (s-b) * (s-c) )
where a, b, and c are the lengths of the sides, and s is the semiperimeter:
s = (a+b+c) / 2
Write a void function that uses five parameters: three value parameters that
provide the length of the edges and two reference parameters that compute the
area and perimeter (not the semiperimeter). Make your function robust. Note
that not all combinations of a, b, and c produce a triangle. Have your program
read an input file named 'triangle.txt' which, on each line, contains the side
lengths. A sample file is available on Blackboard. You can assume that the
sides are all integers (and semiperimeter and area are doubles). Your output
should be similar to the following:
Output:
a=3, b=5, c=7, s=7.5, A=6.49519
a=5, b=7, c=9, s=10.5, A=17.4123
a=10, b=7, c=8, s=12.5, A=27.8107
a=9, b=7, c=3, s=9.5, A=8.78564
a=5, b=8, c=5, s=9, A=12
a=7, b=3, c=5, s=7.5, A=6.49519
a=4, b=6, c=8, s=9, A=11.619
The triangle.txt file is simply:
3 5 7
5 7 9
10 7 8
9 7 3
5 8 5
7 3 5
4 6 8
I am not sure what I am doing wrong but I think maybe my fstream or using a if statement is wrong. Any help is appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include<iostream>
#include<cmath>
#include <fstream>
using namespace std;
int main()
{
double area,perimeter;
int finish,a,b,c;
bool compute_area(double&,double&,double,double,double);
fstream inputFile;
inputFile.open("triangle.txt");
while (inputFile >> a >> b >> c){
cout << "a=" << a << " b=" << b << " c=" << c << endl;
bool valid = compute_area(area,perimeter,a,b,c);
if (valid)
{
cout << "The area of a triangle with sides of length " << a
<< " , " << b << " and " << c << " is " << area <<
"\nand the perimeter is "<<perimeter<< endl;
}
cin>>finish;
return 0;
}
bool compute_area (double& area, double& perimeter, double a, double b, double c) {
double s;
if( (a<(b+c))&&(b<(a+c))&&(c<(b+a)) )
{
s = (a + b + c)/ 2.0;
area = sqrt (s*(s-a)*(s-b)*(s-c));
perimeter=a+b+c;
}
else
{
cout<<"bad combination!"<<endl;
return false;
}
return true;
}
|
I forgot to mention I was supposed to use void functions but I didn't in the code because I am not sure to implement them in the code.