Question

In: Computer Science

Write a C++ program that does the following: Read and input file containing the following PersonAName,...

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

Solutions

Expert Solution

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


Related Solutions

Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
Write a C++ program to read a data file containing the velocity of cars crossing an...
Write a C++ program to read a data file containing the velocity of cars crossing an intersection. Then determine the average velocity and the standard deviation of this data set. Use the concept of vector array to store the data and perform the calculations. Include a function called “Standard” to perform the standard deviation calculations and then return the value to the main function for printing. Data to use. 10,15,20,25,30,35,40,45,50,55. Please use something basic.
Write a c++ program that does the following, read temperatures from a file name temp.txt into...
Write a c++ program that does the following, read temperatures from a file name temp.txt into an array, and after reading all the temperatures, output the following information: the average temperature, the minimum temperature, and the total number of temperatures read. Thank you!
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file...
Write a program that takes three sets ’A’, ’B’, ’C’ as input read from the file prog2 input.txt. The first line of the file corresponds to the set ’A’, the second line is the set ’B’, and the third line is the set ’C’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file...
Write a modularized, menu-driven program to read a file with unknown number of records. Input file has unknown number of records of inventory items, but no more than 100; one record per line in the following order: item ID, item name (one word), quantity on hand , and a price All fields in the input file are separated by a tab (‘\t’) or a blank ( up to you) No error checking of the data required Create a menu which...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
Write a program that takes two sets ’A’ and ’B’ as input read from the file...
Write a program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the file...
Write a program that will read in from input file one line at a time until...
Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespaces, commas and periods....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT