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

In this program, you create a map and find the route from a source location to a

ID: 3867606 • Letter: I

Question

In this program, you create a map and find the route from a source location to a destination location. You will use structure(s), pointer(s) and class(es) to accomplish this task. Let's begin by looking at the following map: The letters of the alphabet represent the locations. Each line represents a highway and each location has one exit highway and one entry highway. You may only leave a location through its exit highway and enter it through its entry highway. (For example The route from location A to D using this map is A rightarrow E rightarrow C rightarrow B rightarrow D) Your code will implement this First, you will begin by creating the map above in your code using class(es), structure(s), pointer(s) and anything else you may want to use. Once you have the map, you will prompt the user to enter new names for the locations represented by A, B, C, D, E, above. Once you have accomplished that, you have created your map. Next, you will prompt the user for the starting location and destination location. You will use the map to figure out and then display the route from the starting location to the destination location. You must keep track of how many times a location is chosen as a destination. You must, then, display the following menu and continue to do so until the user decides to exit the program: C - to enter another source & destination pair P - to see the most popular destination locations E - to exit If the user enters P, you must display the locations from most popular (chosen the most number of times as destination) to least popular. If two or more locations have been chosen as the destination the same number of times, list them in alphabetical order. Please remember to place appropriate error checks.

Explanation / Answer

//This codes ask you to create your directed graph and then perform the operation asked.

//Use numerical digits for locations such for A use 0 and for B use 1 and so on.

//On inputing cities, when all are inputed enter -1 -1 as edges for termination and proceed.

#include<bits/stdc++.h>
using namespace std;

struct save{
int d;
struct save *next;
};

struct save *head=NULL, *ptr;

void save_dst(int d){

struct save *curr;

curr = (struct save*)malloc(sizeof(struct save));
curr->d = d;
curr->next = NULL;

if(head==NULL)
head = curr;
else
ptr->next = curr;
ptr = curr;
}
void disp(){
struct save *tmp;
tmp = head;
while(tmp!=NULL){

cout<<tmp->d<<" ";
tmp = tmp->next;
}
}

class path{

public:
void find_path(vector<int> a[], int src, int dst, int n);
};

void path::find_path(vector<int> a[], int src, int dst, int n){

int i,j;
save_dst(dst);
cout<<"Path for requested source and destination ";
while(src!=dst){
for(i=0;i<n;i++){
if(a[src][i]==1){
cout<<src<<" ";
src = i;
}
}
}
cout<<dst;
}

int main(){

int n,i,j,u=0,v=0;
cout <<"Enter size of map=>";
cin>>n;

vector<int> mp[n];
for(i=0; i<n; i++)
for(j=0; j<n; j++)
mp[i].push_back(0);

cout<<"Enter locations connectivity and enter -1 -1 when done:=> ";

while(1){

cout<<"Enter locations connectivity :=>"; //Enter 2 numbers with one <space>
cin>>u>>v;
if(u<0)
break;
mp[u][v] = 1;

}
cout<<"Enter source and Destination to find path=>";
cin>>u>>v;
path p;

p.find_path(mp, u, v, n);

while(1){

char ch;

cout<<" C - to enter another source and destination pair ";
cout<<"P - to see the most popular destination locations ";
cout<<"E - to exit ";
cout<<"Enter your choice:-";
cin>>ch;

switch(ch){

case 'C': cout<<"Enter source and Destination to find path=>";
cin>>u>>v;
p.find_path(mp, u, v, n);
break;
case 'P': disp();
break;

case 'E': exit(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