In: Computer Science
Write a C++ program that does the following:
Read and input file containing the following
PersonAName, PersonBName, XA,YA, XB, YB
where the coordinates of PersonA in a 100 by 100 room is XA, YA
and the coordinates of PersonB is XB, YB.
Use square root function in cmath sqrt() to calculate the shortest distance between two points.
A file will be uploaded in this assignment that will list coordinates of two people.
The program should use a function call that returns the distance. If the distance is less than 6 feet, the report the case giving the names and the calculated distance.
Blackboard will be updated with a data file by Saturday morning.
Data fields
Person A Name Column 1-10
Person B Name Column 11-20
Person A X coordinate 21-23
Person A Y coordinate 25-27
Person B X coordinate 31-33
Person B Y coordinate 35-37
Attached file coordinates.txt
1---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8 Apple John 5 2 3 4 Apple Ben 5 2 10 4 Apple Carla 5 2 4 3 Apple Sonny 5 2 70 55 Tom Jerry 24 34 29 39 Tom Tim 24 34 50 55 Tom Tracy 24 34 88 31 Tom Tammy 24 34 87 90 Jim Tammy 74 89 87 90 James Tammy 72 88 87 90 Josh Tammy 59 24 87 90 Barry Tom 12 78 13 65 Barry Carla 12 78 4 3 Barry John 12 78 3 4 Barry Sonny 12 78 70 55 Barry Jerry 12 78 29 39 Barry Juan 12 78 14 80 Ann Margaret 25 44 28 79 Ann Silvia 25 44 25 55
First, you have to craete a coordinates.txt file in the same directory and add below data in it :
Apple John 5 2 3 4
Apple Ben 5 2 10 4
Apple Carla 5 2 4 3
Apple Sonny 5 2 70 55
Tom Jerry 24 34 29 39
Tom Tim 24 34 50 55
Tom Tracy 24 34 88 31
Tom Tammy 24 34 87 90
Jim Tammy 74 89 87 90
James Tammy 72 88 87 90
Josh Tammy 59 24 87 90
Barry Tom 12 78 13 65
Barry Carla 12 78 4 3
Barry John 12 78 3 4
Barry Sonny 12 78 70 55
Barry Jerry 12 78 29 39
Barry Juan 12 78 14 80
Ann Margaret 25 44 28 79
Ann Silvia 25 44 25 55
Main file code is as below :
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
//function to calculate distance between two persons
double calculateDistance (int XA, int XB, int YA, int YB)
{
return sqrt (pow (XA - XB, 2) + pow (YA - YB, 2) * 1.0);
}
int main ()
{
string personAname, personBname; //name of two persons
int XA, YA, XB, YB; //cordinates of both person A and B
double distance; //distance between two persons
ifstream inputFile; //to read input file
inputFile.open ("coordinates.txt"); //open inputFile of given name
cout << "Report : " << endl;
//if inputFile opens successfully
if (inputFile.is_open ())
{
//to read all the lines 4 times
while (!inputFile.eof ())
{
//take data from inputFile
inputFile >> personAname >> personBname >> XA >> YA >> XB >> YB;
//function call to calculate distance
distance = calculateDistance (XA, XB, YA, YB);
//if distance < 6 then, print name of them with distance
if (distance < 6)
{
cout << "Distance between " << personAname << " and " <<
personBname << " is : " << distance << endl;
}
}
inputFile.close ();
}
//if inputFile does not open successfully
else
{
cout << "Unable to open file";
}
return 0;
}
Code and output :
Input file :
If you want a report in another file then follow this code :
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
//function to calculate distance between two persons
double calculateDistance(int XA, int XB, int YA, int YB)
{
return sqrt(pow(XA - XB, 2) + pow(YA - YB, 2));
}
int main () {
string personAname, personBname; //name of two persons
int XA, YA, XB, YB; //cordinates of both person A and B
double distance; //distance between two persons
ifstream inputFile; //to read input file
ofstream outputfile; //to store output in outputfile
inputFile.open("coordinates.txt"); //open inputFile of given name
outputfile.open("report.txt");
outputfile << "Report : " << endl;
//if inputFile opens successfully
if (inputFile.is_open())
{
//to read all the lines 4 times
while(!inputFile.eof())
{
//take data from inputFile
inputFile >> personAname >> personBname >> XA >> YA >> XB >> YB;
//function call to calculate distance
distance = calculateDistance(XA, XB, YA, YB);
//if distance < 6 then, print name of them with distance
if(distance < 6)
{
outputfile << "Distance between " << personAname << " and " << personBname << " is : " << distance << endl;
}
}
inputFile.close();
}
//if inputFile does not open successfully
else
{
cout << "Unable to open file";
}
return 0;
}
report.txt file wii be created and in that data will be as below image