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

Hi, I need to fix my C++ program so that it can work on visual studio 2015 and i

ID: 3717435 • Letter: H

Question

Hi, I need to fix my C++ program so that it can work on visual studio 2015 and it can output the file.txt like that:

Data Set 1: Test Run 1

Run the program using SF as the start vertex. Program input/output dialog is shown below. The user input is shown in bold.

Enter Start Vertex: SF

Enter End Vertex :LONDON

Min Path: SF LA LONDON 780

Data Set 1: Test Run 2

Enter Start Vertex: SF

Enter End Vertex : PARIS

Min Path: SF LA LONDON PARIS 920

file.txt

0 SF

1 LA

2 CHICAGO

3 NY

4 PARIS

5 LONDON

-1

0 1 80

0 2 200

0 3 300

1 2 230

1 5 700

2 3 180

3 4 630

3 5 500

4 5 140

-1

Code:

#include<iostream>

#include <cstring>

#include<vector>

#include<string>

#include<list>

#include<algorithm>

#include<fstream>

using namespace std;

// create a vector

vector<pair<int, int> > adjanc[6];

// set

int Value = 6;

// define a method

void AddingToEdge(vector <pair<int, int> > adj[], int u, int ver, int wgt)

{

adj[u].push_back(make_pair(ver, wgt));

adj[ver].push_back(make_pair(u, wgt));

}

// define a method

void DisplayGraph(vector<pair<int, int> > adj[], int Ver)

{

int vt, wg;

for (int u = 0; u < Ver; u++)

{

cout << u << " - ";

for (auto it = adj[u].begin(); it != adj[u].end(); it++)

{

vt = it->first;

wg = it->second;

cout << vt << " ="<< wg << " ";

}

cout << " ";

}

}

int ShortestPath(int src, int dest)

{

bool *visit = new bool[2 * Value];

int *Parent = new int[2 * Value];

static int Total = 0;

for (int x = 0; x < 2 * Value; x++)

{

visit[x] = false;

Parent[x] = -1;

}

list<int> queue;

visit[src] = true;

queue.push_back(src);

while (!queue.empty())

{

int str = queue.front();

if (str == dest)

return Total;

queue.pop_front();

for (auto it = adjanc[str].begin(); it != adjanc[str].end(); it++)

{

if (!visit[it->first])

{

visit[it->first] = true;

queue.push_back(it->first);

Total += it->second;

Parent[it->first] = str;

}

}

}

return Total;

}

// main function

int main()

{

string Line;

ifstream file("file.txt");

vector<string> verx;

int num = 1, Ver;

vector<pair<int, int> > adj[6];

if (file.is_open())

{

while (getline(file, Line))

{

while (Line != "-1")

{

char *tken = strtok(const_cast<char*>(Line.c_str())," ");

tken = strtok(NULL, " ");

verx.push_back(tken);

getline(file, Line);

}

Ver = verx.size();

getline(file, Line);

while (Line != "-1")

{

char *token = strtok(const_cast<char*>(Line.c_str()), " ");

int us = atoi(token);

int ver = atoi(strtok(NULL, " "));

int wgt = atoi(strtok(NULL, " "));

AddingToEdge(adj, us, ver, wgt);

getline(file, Line);

}

}

file.close();

}

else cout << "Cannot open file";

string start, end;

cout << "Enter Start Vertex : ";

cin >> start;

cout << "Enter End Vertex : ";

cin >> end;

int str, e;

int x = 0;

for (auto itr = verx.begin(); itr != verx.end(); itr++)

{

if (*itr == start) str = x;

if (*itr == end) e = x;

x++;

}

cout << ShortestPath(str, e) << endl;

DisplayGraph(adj, Ver);

system("pause");

return 0;

}

Explanation / Answer

#include "stdafx.h"
#include<iostream>
#include <cstring>
#include<vector>
#include<string>
#include<list>
#include<algorithm>
#include<fstream>

using namespace std;

// create a vector
vector<pair<int, int> > adjanc[6];
// set
int Value = 6;
// define a method

void AddingToEdge(vector <pair<int, int> > adj[], int u, int ver, int wgt)
{
adj[u].push_back(make_pair(ver, wgt));
adj[ver].push_back(make_pair(u, wgt));
}

// define a method
void DisplayGraph(vector<pair<int, int> > adj[], int Ver)
{
int vt, wg;
for (int u = 0; u < Ver; u++)
{
cout << u << " - ";
for (auto it = adj[u].begin(); it != adj[u].end(); it++)
{
vt = it->first;
wg = it->second;
cout << vt << " ="<< wg << " ";
}
cout << " ";
}
}

int ShortestPath(int src, int dest)
{
bool *visit = new bool[2 * Value];
int *Parent = new int[2 * Value]
static int Total = 0;
for (int x = 0; x < 2 * Value; x++)
{
visit[x] = false;
Parent[x] = -1;
}
list<int> queue;
visit[src] = true;
queue.push_back(src);
while (!queue.empty())
{
int str = queue.front();
if (str == dest)
return Total;
queue.pop_front();
for (auto it = adjanc[str].begin(); it != adjanc[str].end(); it++)
{
if (!visit[it->first])
{
visit[it->first] = true;
queue.push_back(it->first);
Total += it->second;
Parent[it->first] = str;
}
}
}
return Total;
}

// main function
int main()
{
string Line;
ifstream file("file.txt");
vector<string> verx;
int num = 1, Ver;
vector<pair<int, int> > adj[6];
if (file.is_open())
{
while (getline(file, Line))
{
while (Line != "-1")
{
char *tken = strtok(const_cast<char*>(Line.c_str())," ");
tken = strtok(NULL, " ");
verx.push_back(tken);
getline(file, Line);
}
Ver = verx.size();
getline(file, Line);
while (Line != "-1")
{
char *token = strtok(const_cast<char*>(Line.c_str()), " ");
int us = atoi(token);
int ver = atoi(strtok(NULL, " "));
int wgt = atoi(strtok(NULL, " "));
AddingToEdge(adj, us, ver, wgt);
getline(file, Line);
}
}
file.close();
}
else cout << "Cannot open file";
string start, end;
cout << "Enter Start Vertex : ";
cin >> start;
cout << "Enter End Vertex : ";
cin >> end;
int str, e;
int x = 0;
for (auto itr = verx.begin(); itr != verx.end(); itr++)
{
if (*itr == start) str = x;
if (*itr == end) e = x;
x++;
}
cout << ShortestPath(str, e) << endl;
DisplayGraph(adj, Ver);
system("pause");
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