Help with Function

I have to create a program with functions to find the area of a pentagon. I run the program but it gives me the wrong answer. Can anyone see what i did wrong.

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
55
56
  #include <iostream>
#include <math.h>
using namespace std;

double calcArea(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double x5, double y5);

double calcLength(double x1, double y1, double x2, double y2);

int main()

{

double x1, y1, x2, y2, x3, y3, x4, y4, x5, y5;

cout << "Enter the cooordinates of the 5 vertices of a pentagon:\n";

cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4 >> x5 >> y5;

double a = calcArea(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5);

cout << "The pentagons area  is = " << a << "\n";

system ("pause");

return 0;

}

double calcArea(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double x5, double y5)
{

double L1 = calcLength(x1, y1, x2, y2);
double L2 = calcLength(x1, y1, x3, y3);
double L3 = calcLength(x2, y2, x3, y3);
double L4 = calcLength(x1, y1, x5, y5);
double L5 = calcLength(x3, y3, x5, y5);
double L6 = calcLength(x4, y4, x5, y5);
double L7 = calcLength(x3, y3, x4, y4);

double s = (L1 + L2 + L3)/2;
double s2 = (L2 + L4 + L5)/2;
double s3 = (L5 + L6 + L7)/2;



return (sqrt(s*(s-L1)*(s-L2)*(s-L3))+ sqrt(s2*(s-L2)*(s-L4)*(s-L5))+ sqrt(s3*(s-L5)*(s-L6)*(s-L7)));

}

double calcLength(double x1, double y1, double x2, double y2)
{
	return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
}


Last edited on
You forgot to ask a question.
Topic archived. No new replies allowed.