Hello, I have the following problem:
Entering coordinates of points on a plain with keyboard (as rows x,y). Analysing this coordinates. Replacing coordinates in vector (point - structure data type). Then calculating distance from the first point to all another points.
I've written the code of this problem using arrays -> formula r=sqrt( (x[i]-x[1])^2+(y[i]-y[1])^2) ), (coordinate plain).
#include <stdio.h>
#include <iostream.h>
#include <math.h>
main()
{
float x[100], y[100];
int i, n;
float r;
cout << "Enter dimension of 1D massives (vectors of X & Y coordinates)\n";
cin >> n;
cout << "Enter X coordinates of points for the 1st 1D massive (vector 1):\n";
for (i = 0; i < n; i++)
{ cin >> x[i];
}
cout << "Enter Y coordinates of points for the 2nd 1D massive (vector 2):\n";
for (i = 0; i < n; i++)
{ cin >> y[i];
}
for (i = 0; i < n; i++)
{ r=sqrt( ((x[i]-x[0])*(x[i]-x[0])+(y[i]-y[0])*(y[i]-y[0])) );
cout << "\n r = " << r;
cout << "\n";
}
return 0;
}
I'm confused about something. So do you want your program to list the distance between adjacent vectors?
Example:
If you type in 3 points (let's callt hem A, B, and C)
It would display 2 numbers
First would be distance from (A to B)
Second would be distance from (B to C)
am I correct in assuming this?
Edit: If that's what you wanted here was my approach, hopefully it makes sense. It appears that whoever taught you vectors didn't give you a clear understanding of it. Not sure why you were trying to use pointers when it's not necessary.
Thank you so much, very helpful. In this case problem can be solved by several ways. Exact wording of problem requires solving by dynamical arrays called "vectors". I agree that it makes better sense using statical arrays in that case.
This is screaming out for a vector class. If you create one, you could give it the required operators (in this case, the subtraction operator for sure) and a magnitude method.
Yes, algorithm is right but tools of your coding and coding of posts above are different (by used of set of instruments). Using definition of vector from vector algebra aren't always necessary.