Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

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

#include

#include

#include

#include

#include

#include

using namespace std;

// create a vector

vector > adjanc[6];

// set

int Value = 6;

// define a method

void AddingToEdge(vector > 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 > 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 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 verx;

int num = 1, Ver;

vector > adj[6];

if (file.is_open())

{

while (getline(file, Line))

{

while (Line != "-1")

{

char *tken = strtok(const_cast(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(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;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago