1 Replies - 1107 Views - Last Post: 19 February 2013 - 04:50 PM Rate Topic: -----

#1 django773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-February 13

task FINDING THE CLOSEST PAIR OF POINTS in C++

Posted 19 February 2013 - 04:35 PM

***im trying to create a program calculate the closet points from a list using closest pair alogirthm .the Output would display the name of 2 points
Your input format will be : name x-value y-value
i my code i have so far is below but doesnt work correctly ...please any help
# include <iostream>
# include <cmath>
# include <iomanip>
    
using namespace std;struct Range
{
 double X_coordinate;
 double Y_coordinate;
};
double length[49995500];
int main()
{
 Range point[10000];
 int Num_Points;
 char letter_character;
 /*************************
  *     Get user input    *
  *************************/
 
cout << "enter number of coordinates you want compared: ";
cin>>Num_Points;
 
 if (Num_Points <= 1)
 {return 0;}
         
  int counter=0;
  for (int i=0;i<Num_Points;i++)
  
  {cout <<"\nNow enter a letter character to label coordinates and enter your (x,y)points separated  :" << endl; 
  cin >> letter_character;
  cin >> point[i].X_coordinate;
  cin >> point[i].Y_coordinate;}
          double low=10000;
          for (int i=0;i<Num_Points;i++)
          {
             for (int j=i;j<Num_Points-1;j++)
             {
                double X=0,Y=0;
                X = point[i].X_coordinate - point[j+1].X_coordinate;
                Y = point[i].Y_coordinate - point[j+1].Y_coordinate;
                if ( X < 0 )
                   X *= -1;
                if ( Y < 0 )
                   Y *= -1;
                double distance;
                distance = pow(X,2) + pow(Y,2);
                length[counter] = sqrt(distance);
                
                if (length[counter] < low)
                   low = length[counter];
                counter++;
				
             }
          }
          
          if (low >= 10000)
          {
             cout<<"endlessness"<<endl;
          }
		  cout << letter_character << " has the smallest distance of : "<< low << endl;
       return 0;
}

***

This post has been edited by Skydiver: 19 February 2013 - 04:37 PM
Reason for edit:: Put code into code tags. Learn to do it yourself in the future.


Is This A Good Question/Topic? 0
  • +

Replies To: task FINDING THE CLOSEST PAIR OF POINTS in C++

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3590
  • View blog
  • Posts: 11,168
  • Joined: 05-May 12

Re: task FINDING THE CLOSEST PAIR OF POINTS in C++

Posted 19 February 2013 - 04:50 PM

If you are going to store the X and Y coordinates of a point into a structure, you should also store the point's name into the structure.

It looks like you are using the brute force approach to solving the closest pair problem. Notice that the brute force solution given in Wikipedia doesn't require an array to keep track of the distances between pairs. http://en.wikipedia...._points_problem
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1