 #1
 85
 2
Homework Statement
I have successfully opened and discarded the headers. Then I calculated the distances between point 1 and others and the minimum distance as well. However, I am not sure how to make the code work for more than 4 points. The attached text file is what I am trying to read and extract data from.
Also, I need to tell which is the closest point to point 1 and the distance between point 1 and the closest point on the output screen, what would be the best way of doing this?
Your help would be appreciated. Thank you.
Homework Equations
The Attempt at a Solution
C:
// Program to read an input file
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
double x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, dist_12, dist_13, dist_14, minimum_distance;
ifstream inFile("inputweek6adq42.txt"); // ifstream function to read the file
string line, c; // To read the characters
char ch;
if (inFile.is_open())
{
getline(inFile, line); // To read the header of the input file then discard it
getline(inFile, line);
inFile >> x1 >> y1 >> z1;
inFile >> x2 >> y2 >> z2;
inFile >> x3 >> y3 >> z3;
inFile >> x4 >> y4 >> z4;
inFile.close();
dist_12 = sqrt((x2  x1)*(x2  x1) + (y2  y1)*(y2  y1) + (z2  z1)*(z2  z1));
dist_13 = sqrt((x3  x1)*(x3  x1) + (y3  y1)*(y3  y1) + (z3  z1)*(z3  z1));
dist_14 = sqrt((x4  x1)*(x4  x1) + (y4  y1)*(y4  y1) + (z4  z1)*(z4  z1));
minimum_distance = min(min(dist_12, dist_13), min(dist_13, dist_14));
cout << "The distance between point 1 and 2 is " << dist_12 << "\n";
cout << "The distance between point 1 and 3 is " << dist_13 << "\n";
cout << "The distance between point 1 and 4 is " << dist_14 << "\n";
cout << "The minimum distance between point 1 and another point is " << minimum_distance << "\n";
}
else
cout << "The file could not be opened." << "\n"; // To check for any error
system("pause");
return 0;
}
Attachments

9.8 KB Views: 152