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=/* ??? */
#include <stdio.h>
#include <iostream.h>
#include <vector.h>

using std::cout;
using std::cin;

//2 dimensional vector struct
struct points
{ 
    float x, y;
};


main()
{ 
  int i; 
  float r;
  std::vector<points*> pointvect; // vector declaration
  points * point; //declare pointer

  cout << "Set dimension of vector";
  cin >> i; //with struct only 2D accepted

  for (i=0; i<i; i++) 
  {  // Input coordinates x, y
    point = new point; //init a new points 

    cout << "Input x:";
    cin >> point->x; //assign values
    cout << "Input y:";
    cin >> point->y;    
    pointvect.push_back(point); // add point to the vector
 }

  //then do calculations as before because the vector can be accessed like an array
  // either with an iterator or by pointvect[pos]->x 
  //r=sqrt(((pointvect[i]->x - pointvect[0]->x)*(pointvect[i]->x - pointvect[0]->x)+(pointvect[i]->y-pointvect[0]->y)*(pointvect[i]->y-pointvect[0]->y)

  return 0;
}

hope this helps

Can you tell how to use iterators in this situation? This code is wrong, but is algorithm right?:

    #include <stdio.h>
    #include <iostream.h>
    #include <vector.h>

    using std::cout;
    using std::cin;

    //2 dimensional vector struct

    struct points
    {
    float x, y;
    };

    main()
    {
    int i;
    float r;
    std::vector<points*> pointvect; // vector declaration
    points * point; //declare pointer

    cout << "Set dimension of vector";
    cin >> i; //with struct only 2D accepted

    for (i=0; i<i; i++)
    { // Input coordinates x, y
    point = new point; //init a new points
    cout << "Input x:";
    cin >> point->x; //assign values
    cout << "Input y:";
    cin >> point->y;

    pointvect.push_back(point); // add point to the vector
    }

    vector<points>::iterator pos;
            for (pos = pointvect.begin(); pos != pointvect.end(); pos++) {
          r=sqrt( (pointvect[i]-pointvect[0])*(pointvect[i]-pointvect[0]));

          cout << r;
          }
          return 0; 
          }

here is the working code

#include <iostream>
#include <vector>

using std::cout;
using std::cin;

//2 dimensional vector struct
struct points
{
   float x, y;
};

main()
{
    int i;
    float r;

    std::vector<points*> pointvect; // vector declaration
    points * point; //declare pointer

    cout << "Set dimension of vector";
    cin >> i; //with struct only 2D accepted

    for (int j=0; j<i; j++)
    { // Input coordinates x, y
        point = new points; //init a new points
        cout << "Input x:";
        cin >> point->x; //assign values
        cout << "Input y:";
        cin >> point->y;
        pointvect.push_back(point); // add point to the vector
    }

    //define iterators
    std::vector<points*>::iterator first = pointvect.begin(); 
    std::vector<points*>::iterator end = pointvect.end(); 

    //you use the iterators in the following way
    for(std::vector<points*>::iterator itr = first; itr != end; itr++)
    {
        cout <<((*itr)->x - (*first)->x) << "\n"; //minus all elements x co-ords from the first x co-ord and print
    }

    //cout << r;


return 0;
}

hope this helps

This question has already been answered. Start a new discussion instead.