-
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&);
void answer (double&, 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]);
answer (centx, centy, area);
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[0];
cin >>y[0];
while (x[n+1]!=x[0] || y[n+1]!=y[0])
{
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;
}
-
pointing at a couple of things:
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void getxy (double&, double&);
void calccent (double&, double&);
void answer (double&, double&, double&);
int main()
{
double centx, centy, area;
int n;
double x[]= {n}; //N has no value X has no size. consider double x[10] = {0}
double y[]= {n};
getxy (x[n],y[n]);
calccent (x[n],y[n]);
answer (centx, centy, area);
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.
-
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&);
void answer (double&, 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]);
answer (centx, centy, area);
system ("PAUSE");
}
void getxy (double x[], double y[])
{
int n;
cout << "Enter x and y separated by a space: ";
cin >> x[0];
cin >>y[0];
while (x[n+1]!=x[0] || y[n+1]!=y[0])
{
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;
}
-
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.
Danny Kalev
Similar Threads
-
By tenpoints in forum VB Classic
Replies: 4
Last Post: 02-14-2006, 08:11 PM
-
By aesoprock00 in forum Java
Replies: 2
Last Post: 02-11-2006, 11:17 PM
-
By peter higgins in forum VB Classic
Replies: 1
Last Post: 02-14-2001, 11:31 AM
-
Replies: 1
Last Post: 09-24-2000, 01:05 AM
-
By Brian Leung in forum VB Classic
Replies: 12
Last Post: 06-20-2000, 03:06 PM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|