Question: Write a program ( in main.cpp ) that: Prompts the user for a filename containing node data. Outputs the nodes of a graph in a

Write a program (in main.cpp) that:
Prompts the user for a filename containing node data.
Outputs the nodes of a graph in a depth first traversal.
info>Note: Files Ch20_Ex13Data.txt and Ch20_Ex1Data.txt contain node data that you may test your program with.
Here is my code:
main.cpp
#include
#include
#include
#include
using namespace std;
void depthFirstTraversal(const vector>& graph, int start){
vector visited(graph.size(), false);
stack s;
s.push(start);
while (!s.empty()){
int node = s.top();
s.pop();
if (!visited[node]){
cout node "";
visited[node]= true;
for (int neighbor : graph[node]){
if (!visited[neighbor]){
s.push(neighbor);
}
}
}
}
}
int main(){
string filename;
cout "Enter filename containing node data: ";
cin >> filename;
ifstream inputFile(filename);
if (!inputFile){
cerr "Error opening file." endl;
return 1;
}
int numNodes;
inputFile >> numNodes;
vector> graph(numNodes);
for (int i =0; i numNodes; ++i){
int node;
inputFile >> node;
while (node !=-999){
graph[i].push_back(node);
inputFile >> node;
}
}
inputFile.close();
cout "Depth First Traversal:" endl;
for (int i =0; i numNodes; ++i){
cout "Starting from node " i ": ";
depthFirstTraversal(graph, i);
cout endl;
}
return 0;
}
the code is working yet the parameter that is being asked is not being approved, please help.
 Write a program (in main.cpp) that: Prompts the user for a

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!