DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Feb 2007
Posts
6

## arrays and functions

ok, i need help with using arrays with functions, the following code is supposed to calculate the centroid and area of a polygon, but so far i can only get it to calculate squat, any help would be appreciated(yes i do know there are several things wrong but do not know how to correct them)

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void getxy (double&, double&);
void calccent (double&, double&);

int main()
{
double centx, centy, area;
int n;
double x[]= {n};
double y[]= {n};
getxy (x[n],y[n]);
calccent (x[n],y[n]);
system ("PAUSE");
}
void getxy (double& x[n], double& y[n])
{
int n;
double x[]= {n};
double y[]= {n};
cout << "Enter x and y separated by a space: ";
cin >> x;
cin >>y;

while (x[n+1]!=x || y[n+1]!=y)
{
int n;
cout <<"Insert x and y separated by a space: ";
cin >> x[n];
cin >> y[n];
n=0;
n++;
}
}
void calccent (double& centx, double& centy, double& x[n], double& y[n], double& area)
{
int n;
area=-((y[n+1]-y[n])*(x[n+1]-x[n]))/2;
centx=-(1/area)*((y[n+1]-y[n])/8))*(pow((x[n+1]+x[n]),2))+(pow((x[n+1]-x[n]),2))/3;
centy=(1/area)*((x[n+1]-x[n])/8))*(pow((y[n+1]+y[n]),2))+(pow((y[n+1]-y[n]),2))/3;
}
void answer (double& centx, double& centy, double& area)
{
cout << "These are the centroid coordinates: " << centx << "," << centy;
cout << "\nThis is area of the polygon: " << area << endl;
}  Reply With Quote

2. Senior Member
Join Date
Dec 2003
Posts
3,366
pointing at a couple of things:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void getxy (double&, double&);
void calccent (double&, double&);

int main()
{
double centx, centy, area;
int n;
double x[]= {n}; //N has no value X has no size. consider double x = {0}
double y[]= {n};
getxy (x[n],y[n]);
calccent (x[n],y[n]);
system ("PAUSE");
}
void getxy (double& x[n], double& y[n]) //arrays are always reference, lose the &
{
int n; //local N overrides the N that was supposedly visible in the header. (??)

Half your troubles are N -- you dont give it a value, its scope is hopping around, etc. Take a good look at every use of N to start with.  Reply With Quote

3. Registered User
Join Date
Feb 2007
Posts
6
well then i guess the question i should ask is if using arrays are justified for the particular purpose i have

i want the user(in this case me)to input any double for x and y and when the x and y first input is input again it terminates the loop and then puts all those values in my equations

taking out the n's and the &'s did help alot tho
updated:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

void getxy (double&, double&);
void calccent (double&, double&);

int main()
{
double centx, centy, area;
int n;
double x[]= {n};
double y[]= {n};
getxy (x[n],y[n]);
calccent (x[n],y[n]);
system ("PAUSE");
}
void getxy (double x[], double y[])
{
int n;
cout << "Enter x and y separated by a space: ";
cin >> x;
cin >>y;

while (x[n+1]!=x || y[n+1]!=y)
{
int n;
cout <<"Insert x and y separated by a space: ";
cin >> x[n];
cin >> y[n];
n=0;
n++;
}
}
void calccent (double& centx, double& centy, double x[], double y[], double& area)
{
int n;
area=-((y[n+1]-y[n])*(x[n+1]-x[n]))/2;
centx=-(1/area)*((y[n+1]-y[n])/8))*(pow((x[n+1]+x[n]),2))+(pow((x[n+1]-x[n]),2))/3;
centy=(1/area)*((x[n+1]-x[n])/8))*(pow((y[n+1]+y[n]),2))+(pow((y[n+1]-y[n]),2))/3;
}
void answer (double& centx, double& centy, double& area)
{
cout << "These are the centroid coordinates: " << centx << "," << centy;
cout << "\nThis is area of the polygon: " << area << endl;
}  Reply With Quote

4. I don't arrays are necessary here. Isn't the value calculated after the user has entered a pair of values? In that case, you can stick to a single pair of double, and simply overwrite this pair on each iteration.
If you want to read multiple pairs, use a vector or some other container. This will free you from the burden of declaring the array's size at compiler time and will make your code simpler.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•