I am trying to test a formula that calculates polygon area. However, I can't seem to compile.
#include <iostream>
using namespace std;
int main (int argc, char** argv)
{
int xvalue[12];
int yvalue[12];
int X = 0;
int Y = 0;
double area = 0.0;
double computeArea(int *, int *, int);
for (int i=0; i<12; i++)
{
cout << "\nPlease enter x-ordinate of pt " << i+1 << ": ";
cin >> X;
xvalue[i] = X;
cout << "Please enter y-ordinate of pt " << i+1 << ": ";
cin >> Y;
yvalue[i] = Y;
}
/*for (int i=0; i<12; i++)
{
computeArea(xvalue[i], yvalue[i], 12);
}*/
area = computeArea(xvalue, yvalue, 12);
cout << "Your area is: " << area << endl;
}
double computeArea(int *X, int *Y, int points)
{
double area;
int i;
int j=points-1;
for (i=0; i<points; i++)
{
area+=(X[j]+X[i])*(Y[j]-Y[i]);
j=i;
}
return area*.5;
}
The script compiles now thanks to R Sahu, so I have input 12 coordinates, which are:
15, 3
15, 5
13, 5
13, 7
15, 7
15, 9
17, 9
17, 7
19, 7
19, 5
17, 5
17, 3
But the results came out as 240
, which is wrong and it should be 20