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

Please help thanks!!!!!! Program Tasks For this project, you need to determine t

ID: 3707798 • Letter: P

Question

Please help thanks!!!!!!

Program Tasks For this project, you need to determine the shortest route for your gLyft driver. The route starts from a given starting point, proceeds to the customers' planets, and ends at the given endpoint. You will need to read in data from input files, determine the route, and create an output file with a "map" of the planets and the route the gLyft driver needs to take to get from the starting point to the end point. These tasks are detailed in the next sections. Input Data There are two input files that are required by your program: 1) a locations file, and 2) a planetary names file. The files themselves are defined in the Input Files Section. The sections below describe the procedures needed to process the data within the input files. Get Filenames Your program must prompt the user for the names of the two input files (this makes the program flexible --the person using it can call the files whatever they want). Prompt the user for the locations file first using "Enter Locations Filename:"; the user then enters the ?location.file>.txt name. Then, prompt the user for the planetary names filename, .txt with "Enter Names Filename: ". Open Files Once both input filenames have been entered by the user, the program must then open the files for reading. If either of the input files is not found or cannot be opened for reading, the program must output "Input file could not be opened" to the terminal followed immediately by (on a separate line) program exit with codereturn 1;. Note that you do not need to determine which file caused the failure; simply print the message and exit the program. Note The provided test cases do not test the error handling of input files. You must generate your own test case to ensure compliance. Do not submit your test case, just ensure your code satisfies this important project specification. Read Data The locations file and the planetary names file are detailed in the Input Files section. Note that the locations file contains the range for that particular gLyft driver (i.e., number of rows and columns

Explanation / Answer

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <fstream>
#include <algorithm>

using namespace std;

struct Point {
    int r, c;
    char sym;
    int id;
};

struct Place {
    int id;
    string n;
};

double dist(double cc, double cr, double pc, double pr){
    double x, y, d;
    x = pow((cc-pc),2);
    y = pow((cr-pr),2);
    d = pow(x+y, 0.5);
    return d;
}

int findAP(vector <int> v, double n){
    int s = 1;
    for(int i = 0; i<v.size(); i++){
        if (v.at(i) == n){
            s = 0;
        }
    }
    return s;
}

int main(){
    //declaring variables
  
    string locfile;
    string namfile;
    double gr, gc, sr, sc, er, ec;
    Point p;
    vector <Point> data (1);
    double n = 0;
    int i, j;
    Place pl;
    int y,z;
    vector <Place> names (0);
    double cx, cy;
    double mind = -1, potd;
    double nx, ny;
    Point npoint, cpoint;
    int index;
    int cid, idi;
    vector <int> useddata (0);
  
  
    //getting file names
    cout<<"Enter Locations Filename: "<<endl;
    getline(cin,locfile);
  
    cout<<"Enter Names Filename: "<<endl;
    getline(cin,namfile);
  
    //opening files and checking for failure
    ifstream inloc;
    inloc.open(locfile.c_str());
    ifstream innam;
    innam.open(namfile.c_str());
    if (inloc.fail() or innam.fail()){
        cout<<"Input file could not be opened"<<endl;
        exit(EXIT_FAILURE);
    }
  
    //reading location file
    inloc>>gr>>gc>>sr>>sc>>er>>ec;
  
    //creating grid
    vector <char> row (gc,'.');
    vector < vector <char> > grid (gr, row);
  
    //placing starting and ending points
    grid.at(sr-1).at(sc-1) = 'S';
    grid.at(er-1).at(ec-1) = 'E';
      
    //reading in safe point information
    inloc>>p.r>>p.c>>p.sym>>p.id;
    if (p.r > gr or p.c > gc or p.r<=0 or p.c<=0){
        cout<<p.id<<" out of bounds - ignoring"<<endl;
        n = 1;
    }
    else {
        data.at(0) = p;
        grid.at(p.r-1).at(p.c-1) = p.sym;
    }
    inloc>>p.r;
    while (! inloc.fail()){
        inloc>>p.c>>p.sym>>p.id;
        if (p.r > gr or p.c > gc or p.r<=0 or p.c<=0){
            cout<<p.id<<" out of bounds - ignoring"<<endl;
        }
        else {
            if (n){
                data.at(0) = p;
                n=0;
            }
            else {
                data.push_back(p);
                grid.at(p.r-1).at(p.c-1) = p.sym;
            }
        }
        inloc>>p.r;
    }
  
    //starting output
    ofstream omap("journey.txt");
  
    //printing map
    for (i=0;i<grid.size();i++){
        for(j=0;j<grid.at(i).size();j++){
            omap<<grid.at(i).at(j);
        }
        omap<<endl;
    }
  
    //reading in place names and correcting names
    innam>>pl.id;
    while (! innam.fail()){
        innam>>pl.n;
        z = pl.n.find("XX");
        while (z != pl.n.npos){
            pl.n.erase(z, 2);
            z = pl.n.find("XX");
        }
        y = pl.n.find("_");
        while (y != pl.n.npos){
            pl.n.erase(y,1);
            pl.n.insert(y," ");
            y = pl.n.find("_");
        }
        names.push_back(pl);
        innam>>pl.id;
    }
  
    //continuing output
    omap<<"Start at "<<sr<<" "<<sc<<endl;
  
  
    //processing data and values
    cx = sc;
    cy = sr;
  
    //main loop to pick next point
    while (useddata.size()<data.size()){
        for(i = 0; i<data.size(); i++){
            potd = dist(cx, cy, data.at(i).c, data.at(i).r);
            if (mind == -1 and findAP(useddata,data.at(i).id)){
                nx = data.at(i).c;
                ny = data.at(i).r;
                npoint = data.at(i);
                index = i;
                idi = data.at(i).id;
                mind = potd;
            }
            else if (potd < mind and findAP(useddata,data.at(i).id)){
                nx = data.at(i).c;
                ny = data.at(i).r;
                npoint = data.at(i);
                index = i;
                idi = data.at(i).id;
                mind = potd;
              
            }
            else if (potd == mind and findAP(useddata,data.at(i).id)){
                if (data.at(i).id < npoint.id){
                    nx = data.at(i).c;
                    ny = data.at(i).r;
                    npoint = data.at(i);
                    index = i;
                    idi = data.at(i).id;
                    mind = potd;
                }
              
            }
        }
        //outputs next point to report
        cx = nx;
        cy = ny;
        cpoint = npoint;
        useddata.push_back(idi);
        for (i = 0; i<names.size(); i++){
            if (idi == names.at(i).id){
                cid = i;
            }
        }
        omap<<"Go to "<<names.at(cid).n<<" at "<<cy<<" "<<cx<<endl;
        mind = -1;
    }
  
    //final output
    omap<<"End at "<<er<<" "<<ec<<endl;
  
    //closing input and output streams
    omap.close();
    innam.close();
    inloc.close();
  
    return 0;
}

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