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;
}
How to write it using vectors?
#include <stdio.h>
#include <iostream.h>
#include <vector.h>
struct {
{ float x, y;
} points;
main()
{ using namespace std;
int i;
float r;
cout << "Set dimension of vector";
cin >> i;
for (i=0; i<i; i++) { // Input coordinates x, y
cout << "Input x:";
cin >> points.x;
cout << "Input y:";
cin >> points.y;
}
std:: vector<points> pointvect; // vector declaration
points points;
pointvect.push_back (points); // add point to the vector
r=/* ??? */