0

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

TKM
  • 45
  • 2
  • 6
  • `computeArea()` expects `int*,int*,int` and you give `int,int,int` – Gaurav Sehgal May 15 '15 at 18:04
  • after i change it to `double computeArea(*int, *int, int);` I faced more errors which are `error: expected primary-expression before ‘int'` and `initializer expression list treated as compound expression` and `‘computeArea’ cannot be used as a function` – TKM May 15 '15 at 18:09

2 Answers2

2

The declaration and the definition don't match.

double computeArea(int, int, int);

double computeArea(int *X, int *Y, int points)

Change the declaration to:

double computeArea(int *, int *, int);

Change the way you call it. Instead of

for (int i=0; i<12; i++)
{
    computeArea(xvalue[i], yvalue[i], 12);
}

use

double area = computeArea(xvalue, yvalue, 12);

To print the area, instead of

cout << "Your area is: " << computeArea() << endl;

use

cout << "Your area is: " << area << endl;

EDIT

You haven't initialized area to 0. That needs to be fixed.

Your formula you are using to compute the area is incorrect. It needs be

    area += (X[j]*Y[i] - X[i]*Y[j]);

See http://mathworld.wolfram.com/PolygonArea.html.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • after i change it to `double computeArea(*int, *int, int);` I faced more errors which are `error: expected primary-expression before ‘int'` and `initializer expression list treated as compound expression` and `‘computeArea’ cannot be used as a function` – TKM May 15 '15 at 18:09
  • but how will the `xvalue` and `yvalue` be right without the `[i]`, since I am going to store 12 sets of X and Y – TKM May 15 '15 at 19:37
  • @TKM, that question indicates to me that you should spend time understanding how to use arrays in C++, using a text book or an online tutorial. – R Sahu May 15 '15 at 19:39
  • just did, still don't get how your method will work – TKM May 15 '15 at 19:49
  • @TKM, try it. Step through the code in a debugger. Maybe that will help clear your doubts. – R Sahu May 15 '15 at 19:51
  • I have edited my question, please take a look, thanks – TKM May 15 '15 at 20:06
  • You have `double computeArea(*int, *int, int);`. That is not right. It should be `double computeArea(int*, int*, int);`. The `*` needs to be after the `int`, not before. – R Sahu May 15 '15 at 20:09
  • Sorry, made a change, please take a look again. – TKM May 15 '15 at 20:16
  • @TKM, see the addition to the answer. – R Sahu May 15 '15 at 21:04
0

This is how you have called your function:

    cout << "Your area is: " << computeArea() << endl;

This is what your function prototype is:

double computeArea(int *X, int *Y, int points)

As you can see, it expects 3 parameters, and you are passing none.

Again, another prototype:

    double computeArea(int, int, int);

The first one uses pointers, the second one uses normal ints.

Hence the warning.

Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43
  • after i change it to `double computeArea(*int, *int, int);` I faced more errors which are `error: expected primary-expression before ‘int'` and `initializer expression list treated as compound expression` and `‘computeArea’ cannot be used as a function` – TKM May 15 '15 at 18:09
  • That is because sometimes you pass integers to the functions, sometimes you pass an array. What exactly does your function want? – Ayushi Jha May 15 '15 at 18:11
  • so for trying my example above, i intend to calculate the area of an cross, so I will input 12 points of X-axis and Y-axis, then base on the coordinates, it will be placed into the function to compute out the area – TKM May 15 '15 at 19:18