Computing areas of a triangle

Alright, Im at the end of my project and I have finally hit another wall. I know that I am very close but need a fresh pair of eyes to see where Im going wrong. Im taking triangles from a file and finding their area then writing them back into a separate file. Im getting very close to the numbers that I need but have no clue on what Im doing wrong. I think its in my area function but not sure. I also get a weird result when I have my if statement using &&. When I use || I get the result I need but it shouldn't be that way.

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
51
52
53
54
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cmath>
using namespace std;

bool isValidTriangle(int a, int b, int c);
double area(int a, int b, int c);
int main()
{
	int a, b, c;
	ifstream edges;
	ofstream angles_area;
	edges.open("edges.txt");
	angles_area.open("areas.txt");
	if (edges.is_open())
	{
		while (edges >> a >> b >> c)
		{
			if (isValidTriangle(a, b, c))
			{
				angles_area << area(a, b, c) <<endl;
			}
			if (!isValidTriangle(a, b, c))
			{
				angles_area << "*"<<endl;
			}
		}

	}
	else
	{
		cout << "Input file open failed.\n";
		exit (1);
	}
	angles_area.close();
	edges.close();
	return 0;
}

bool isValidTriangle(int a, int b, int c)
{
	if (((a + b) < c) && ((b+c)<a) && (a+c)<b)//getting a weird result here.
	{
		return false;
	}
	else return true;
}
double area(int a, int b, int c)
{
	int s;
	s = ((a + b + c)/2);
	return sqrt(s*(s - a)*(s - b)*(s - c));//I think the error might be here
}


The file is:
12 15 7
8 9 14
11 2 15
6 5 4
18 7 10
16 11 17

And Im getting:
41.2311
25.0998
-1.#IND (This should be "*")
6.48074
-1.#IND (This should be "*")
85.2056
The file is a list of edge lengths?

Line 43 cannot be true for all cases. What you should be checking is that the sum of the two shortest edges is not greater than the longest edge.

The error you are having with Heron's formula is actually a problem with C and C++ that bites people all the time.

On line 52 you take the sum of three integers, then do an integer division on them and assign the result to an integer.

The first thing to do is make s a double. Then make sure the division is a floating point one.

51
52
53
	double s;
	s = ((a + b + c)/2.0);
	return sqrt(s*(s - a)*(s - b)*(s - c));

Hope this helps.
closed account (SECMoG1T)
Okay lemmi ask one question according to your rules here
(((a + b) < c) && ((b+c)<a) && (a+c)<b) what conditions exactly are you using to define a triangle ? Are you sure that there are values that will meet all the 3 conditions at the same time ?
Last edited on
We're mixing ands and ors here!

A triangle is only valid each pair of sides combined are greater than the third:

if (a + b > c && b + c > a && a + c > b)

it is invalid if any two sides combined are less than the third:

if (a + b < c || b + c < a || a + c < b)

chose one and make it go.

Because we're only using one boolean operator, only one set of () is necessary.
Last edited on
That was the issue Duoas. It was that double and the 2.0 in my function. andy1992 I was using the logic that with any triangle the sum of the lengths of any two edges shall be greater than the length of the remaining edge. Which was given in the problem.
Topic archived. No new replies allowed.