"Nutkin" <ja*******@dsl. pipex.comwrote in message
news:11******** *************@j 44g2000cwa.goog legroups.com...
Basicly i have to make a program that calculates the distance between x
and y points in 2d space.
the code basicly goes like this
1. User says how many points they have (max of 10)
Okay, good. Store this in a variable.
2. User enters points
Okay, good. Store these values somewhere. A std::vector would be good, but
if that is beyond you, a dynamic array.
3. Using sqrt( (x2-x1)^2 + (y2-y1)^2) ) It calculates the distance
between 2 points
Which two? You entered 2 to 10 points. All of them to each other? 2-3,
then 3-4, then 4-5 ?
4. It displays the length between the first and last point.
Oh, I see. I think you want to total the paths. It's not truely the
distance between the first point and the last point, but the total of the
distance with the points inbetween.
My problem is how do i accept the data. im not sure how to vary the
number of inputs;
A for loop or while loop works for this.
or how to declare the variables. like say the user
wants 6 points how do i let the program know only to ask the user for 6
points. and then how do i do the same calculation for each of those
points.
A vector or a dynamic array works for this.
i tried using a while loop and heres my code so far.
#include <iostream>;
#include <cmath.h>
using namespace std;
double length(double xa,double xb,double ya,double yb)
{
double length=0;
length=sqrt(((x b-xa)*(xb-xa))+((yb-ya)*(yb-ya)));
return (length);
}
int main()
int points=0;
int ans=0;
double length(double,d ouble,double,do uble)
double xa=0;
double xb=0;
double ya=0;
double yb=0;
cout <<"How many points would you like to input (Max 10)?\n\n";
cin >>points;
while (points 1)
{
cout <<"Please enter an x value\n";
cin >>xa;
cout <<"Please enter a y value\n";
cin >>ya;
cout <<"Please enter an x value\n";
cin >>xb;
cout <<"Please enter a y value\n";
cin >>yb;
ans=ans+length( xa,xb,ya,yb)
points=points-1;
}
return (0);
Im using VC++
i know the codes a little crappy but hey thats what help is for right
:)
Well, your program would work with a little modification, but what about now
you want to know the average of the distances, etc? You are not storing the
points.
Two ways, like I said. A dynamic array, or a std::vector. I believe in
your case a std::vector would be prefered.
But, what do we push onto the vector? A point has two values, an X and a Y.
You could use std::pair, but a structure is probably prefered.
(untested code)
struct Point2D
{
double X;
double Y;
};
std::vector<Poi nt2DPoints;
Okay, instead of a while loop I would use a for loop.
for ( int i = 0; i < points; ++i )
// It is customary to start counting in 0 in C++ because arrays are 0 bound
{
std::cout >"Enter point number:" << i << ":";
Point2D Point;
std::cin >Point.X >Point.Y;
// Note, if they entered a bad value, such as "blah" then X and Y
have
// bad data. Excersice for the reader to deal with this //
Points.push_bac k( Point );
}
At this point, if all went well, you have a std::vector of Point2D that
cointains points number of points. You can access them with subscript
operator such as:
Points[0].X
Points[0].Y
etc...
You can also access them using iterators.
std::vector<Poi nt2D>::iterator it = Points.begin();
(*it).X
(*it).Y
I generally use iterators, but for simplsticy sake, we'll use a for loop
again.
double TotalDistance = 0;
for ( int i = 0; i < points - 1; ++i )
// I would actually use
for ( size_t i = 0; i < Points.size() - 1; ++i )
// Whichever you understand better, but you should get to know both forms.
{
TotalDistance += length( Ponnts[i].X, Points[i].Y, Points[i+i].X,
Points[i+1].Y );
}
and there's your answer.
Read through all the replies given you and try to understand all of them.
If you're going to be using C++ you are going to need to learn the
containers, such as std::vector.