Problem

(Points nearest to each other) Listing, FindNearestPoints.cpp, is a program that finds two...

(Points nearest to each other) Listing, FindNearestPoints.cpp, is a program that finds two points in a two-dimensional space nearest to other. Revise the program so that it finds two points in a three-dimensional space nearest to other. Use a two-dimensional array to represent the points. Test the program using the following points:

double points[][3] = {{-1, 0, 3}, {-1, -1, -1}, {4, 1, 1},{2, 0.5, 9}, {3.5, 2, -1}, {3, 1.5, 3}, {-1.5, 4, 2},{5.5, 4, -0.5}};

The formula for computing the distance between two points (x1, y1, z1) and (x2, y2, z2) is

Listing FindNearestPoints.cpp

1 #include 2   #include 3   using namespace std;45  // Compute the distance between two points (x1, y1) and (x2, y2)6  double getDistance(double x1, double y1, double x2, double y2)7  {8   return sqrt((x2 − x1) * (x2 − x1) + (y2 − y1) * (y2 − y1));9  }1011  int main()12  {13  const int NUMBER_OF_POINTS = 8 ;1415  // Each row in points represents a point16  double points[NUMBER_OF_POINTS][ ];1718  cout << "Enter " ; << NUMBER_OF_POINTS << " points: " ;19  for (int i = 0 ; i> points[i][0 ] >> points[i][1 ];2122  // p1 and p2 are the indices in the points array23  int p1 = 0 , p2 = 1 ; // Initial two points24  double shortestDistance = getDistance(points[p1][0 ], points[p1][1 ],25  points[p2][0 ], points[p2][1 ]); // Initialize shortestDistance26  27 // Compute distance for every two points28 for (int i = 0 ; ifor (int j = i + 1 ; jdouble distance = getDistance(points[i][0 ], points[i][1 ],33  points[j][0 ], points[j][1 ]); // Find distance3435  if (shortestDistance > distance)36  {37  p1 = i; // Update p138  p2 = j; // Update p239  shortestDistance = distance; // Update shortestDistance40  }41  }42 }4344 // Display result45 cout << "The closest two points are " <<46  "(" << points[p1][0 ] << ", " << points[p1][1] << ") and (" <<47  points[p2][0 ] << ", " << points[p2][1] << ")" << endl;4849 return 0 ;50  }

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT