[ create a new paste ] login | about

Link: http://codepad.org/mNXrJb9z    [ raw code | fork ]

C++, pasted on Mar 4:
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
using namespace std;

#define ArraySize 4

struct Point
{
  Point(double xx, double yy):x(xx),y(yy){}
  
  static double distance(const Point &p1, const Point &p2)
  {
     double dx = p2.x - p1.x;
     double dy = p2.y - p1.y;
     return sqrt(dx*dx + dy*dy);
  }
  double x;
  double y;
};

void CalcDistance(vector<Point> pointsInput, vector<double>& points, int size)
{
  int num = 0;
  int min = 0,p1 = 0, p2 = 0;
  for( int i = 0; i< size - 1; i++)
   {
    for ( int j = i+1; j< size; j++)
      {
        points.push_back(Point::distance(pointsInput[i],pointsInput[j]));
        cout<<"point "<<i+1 <<" and Point "<<j+1 <<" = " <<points.at(num) <<endl;
        if(num == 0 )
        {
        min = points.at(0);      
        }
        else if( points.at(num) < min )
        {
        min = points.at(num);
        p1 = i + 1;
        p2 = j + 1;
        }
         num++;
      }
   }
   
   cout<<"The closest pair are "<<"point "<<p1 <<" and point "<<p2<<endl;
   cout<< "The distance is "<< min << endl;
}


int main(int argc, char *argv[])
{
   vector<Point> myPoints;
   int count = 0;
   double x;
   double y;
   
   while( count < ArraySize )
   {
     cin >> x >> y;
     Point point(x,y);
     myPoints.push_back(point);
     count++;
   }
   
   vector<double> output;
   CalcDistance(myPoints,output,ArraySize);  
  return 0;
}


Create a new paste based on this one


Comments: