Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The distance between two points in 3-space is given by the formula: In C++, the

ID: 3787609 • Letter: T

Question

The distance between two points in 3-space is given by the formula:

In C++, the std::sqrt() function is defined in <cmath>.

The attached file has six integers per line representing the three coordinates each of a pair of points in 3-space.

Write a program to determine how many pairs of points are greater than 10 units apart. Display the result as:

points read:___
points with distance greater than 10.0: ___

Use the following guidelines.

represent each point by an array of three integer coordinates

calculate the distance with a function that takes three parameters: two points, and the number of coordinates for a point

read a pair of points with a function whose signature is bool readPoints(std::ifstream& ifs, int p1[], int p2[], int sz)

return false on end-of-file, true otherwise

2 5 2 8 5 2
9 0 0 1 3 0
0 8 8 9 6 3
3 9 1 8 5 2
1 4 10 0 0 9
9 8 1 3 6 8
9 10 7 3 2 5
4 8 1 4 10 6
0 0 5 8 5 9
4 1 3 9 1 9
8 5 1 10 5 8
4 1 10 3 2 0
2 0 2 4 6 10
10 7 10 10 2 8
4 10 8 1 3 8
5 5 7 6 6 2
4 3 7 4 2 6
1 8 9 5 4 7
5 4 0 5 4 0
1 8 6 2 10 7
1 1 8 4 5 5
3 5 1 2 0 5
4 7 3 9 0 3
7 3 2 0 0 7
7 5 6 4 9 7
3 4 3 3 9 2
7 1 0 3 0 2
9 8 2 4 10 4
1 10 1 6 0 5
3 5 9 3 4 1
9 5 7 9 4 6
1 1 3 4 7 8
0 3 7 8 10 7
0 8 5 10 7 8
4 1 7 7 7 3
5 3 1 4 4 10
7 7 0 10 3 9
4 5 6 5 2 2
10 8 4 1 9 0
10 0 6 10 2 3
2 10 8 8 4 4
7 1 5 2 1 10
0 6 1 9 0 0
3 7 10 0 5 3
2 0 6 6 6 10
7 5 6 5 8 9
8 2 1 9 1 3
7 5 10 3 0 10
5 6 1 1 3 5
1 0 0 0 9 1
3 7 6 8 9 9
6 9 2 1 6 8
5 2 10 5 1 8
0 9 0 8 7 8
5 10 10 0 9 1
4 9 4 7 0 4
8 8 0 10 3 8
8 9 7 5 6 0
1 9 3 10 7 4
9 7 4 5 1 5
9 7 0 9 8 1
6 3 9 0 1 3
7 4 3 7 9 4
1 4 7 6 8 6
9 4 1 3 3 6
6 3 8 6 3 0
0 8 10 3 9 7
3 9 10 0 2 6
2 2 3 4 4 9
8 9 9 4 3 4
9 9 4 10 0 6
1 3 3 10 5 7
10 7 9 5 0 9
6 3 5 7 5 3
0 7 10 10 9 3
8 2 1 9 6 8
2 7 1 1 8 1

Explanation / Answer

main.cpp

#include <iostream>
#include <fstream>
#include <math.h>

using namespace std;
#define MAX 5000

bool readPoints(std::ifstream &ifs, int* x, int* y);
double calculateDistance(int* x, int* y, int noOfCordinatesForAPoint);

int main()
{
   // reading from abc.txt file in same directory
std::ifstream inputfile("abc.txt");

   // creating 2-D array to store 3 coordinates of X-axis
int **xCoordinates = new int*[MAX];
for(int i = 0; i < MAX; ++i) {
xCoordinates[i] = new int[3];
}

   // creating 2-D array to store 3 coordinates of Y-axis
int **yCoordinates = new int*[MAX];
for(int i = 0; i < MAX; ++i) {
yCoordinates[i] = new int[3];
}

   // creating 1-D array to store Distances between individual points(X,Y) respectively
double *distances = new double[MAX];

  // to count no of points found in input file
int noOfPointsFound = 0;

   // keep reading till input points are present. Store the points and then calculate distance between the,
while(readPoints(inputfile, xCoordinates[noOfPointsFound], yCoordinates[noOfPointsFound])) {
distances[noOfPointsFound] = calculateDistance(xCoordinates[noOfPointsFound], yCoordinates[noOfPointsFound],3);
noOfPointsFound++;
}

   // output input points
cout << "points read:" << endl;

int i=0;
while(i < noOfPointsFound) {
cout << xCoordinates[i][0] << " " << xCoordinates[i][1] << " " << xCoordinates[i][2]
<< " " << yCoordinates[i][0] << " " << yCoordinates[i][1] << " " << yCoordinates[i][2] << endl;
i++;
}

   // output resultant points if distance is greater than 10
cout << " points with distance greater than 10.0:" << endl;
i=0;
while(i < noOfPointsFound) {
if(distances[i] > 10) {
cout << xCoordinates[i][0] << " " << xCoordinates[i][1] << " " << xCoordinates[i][2]
<< " " << yCoordinates[i][0] << " " << yCoordinates[i][1] << " " << yCoordinates[i][2] << endl;
}
i++;
}

return 0;
}

bool readPoints(std::ifstream &ifs, int* x, int* y) {
int x1, x2, x3, y1, y2, y3;

if (ifs >> x1 >> x2 >> x3 >> y1 >> y2 >> y3)
{
x[0] = x1;
x[1] = x2;
x[2] = x3;
  
y[0] = y1;
y[1] = y2;
y[2] = y3;
return true;
}
return false;
}

double calculateDistance(int* x, int* y, int noOfCordinatesForAPoint) {
int i;
double distance = 0;
for(i=0; i< noOfCordinatesForAPoint; i++) {
distance += pow((x[i] - y[i]), 2);
}
distance = sqrt(distance);
return distance;
}

abc.txt(Sample input file)

1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 18 17 16
1 2 3 9 9 9

For the above sample data in question, program outputs:

points with distance greater than 10.0:                                                                                                                                                     

0 8 8 9 6 3

9 10 7 3 2 5   

0 0 5 8 5 9

4 1 10 3 2 0

2 0 2 4 6 10   

1 10 1 6 0 5   

0 3 7 8 10 7   

0 8 5 10 7 8

7 7 0 10 3 9

0 6 1 9 0 0   

0 9 0 8 7 8   

5 10 10 0 9 1   

1 3 3 10 5 7   

0 7 10 10 9 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote