A chegg expert answered my computer science question and I am receiving an error
ID: 3720658 • Letter: A
Question
A chegg expert answered my computer science question and I am receiving an error that states "'it' does not name a type." on line 253. He appears to use the data type auto and I am unfamiliar with this. Does anyone know if I need to include something at the beginning of the code to be able to use auto? Or how to fix the error? The code is shown below.
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
struct Edge
{
int src, dest, cost,duration;
};
// a structure to represent a connected, directed and
// weighted graph
struct Graph
{
// V-> Number of vertices, E-> Number of edges
int V, E;
// graph is represented as an array of edges.
struct Edge* edge;
};
// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E)
{
struct Graph* graph = new Graph;
graph->V = V;
graph->E = E;
graph->edge = new Edge[E];
return graph;
}
// The main function that finds shortest distances from src to
// all other vertices using Bellman-Ford algorithm. The function
// also detects negative weight cycle
void BellmanFord(struct Graph* graph, int src, int dest, int costLimit, int &totalDuration)
{
int V = graph->V;
int E = graph->E;
int *dist = new int[V];
// Step 1: Initialize distances from src to all other vertices
// as INFINITE
for (int i = 0; i < V; i++)
dist[i] = INT_MAX;
dist[src] = graph->edge[src].cost;
// Step 2: Relax all edges |V| - 1 times. A simple shortest
// path from src to any other vertex can have at-most |V| - 1
// edges
for (int i = 1; i <= V - 1; i++)
{
for (int j = 0; j < E; j++)
{
int u = graph->edge[j].src;
int v = graph->edge[j].dest;
int cost = graph->edge[j].cost;
int dur = graph->edge[j].duration;
if (dist[u] != INT_MAX && dist[u] + cost < costLimit) {
dist[v] = dist[u] + cost;
totalDuration += dur;
}
}
}
//// Step 3: check for negative-weight cycles. The above step
//// guarantees shortest distances if graph doesn't contain
//// negative weight cycle. If we get a shorter path, then there
//// is a cycle.
for (int i = 0; i < E; i++)
{
int u = graph->edge[i].src;
int v = graph->edge[i].dest;
int cost = graph->edge[i].cost;
int dur = graph->edge[i].duration;
if (dist[u] != INT_MAX && dist[u] + cost < dist[v])
printf("Graph contains negative weight cycle");
}
}
// Driver program to test above functions
int main(int argc, char*argv[])
{
if (argc < 5) {
cout << "Not valid argument";
return 1;
}
int V; // Number of vertices in graph
int E; // Number of edges in graph
vector<Edge> v;
int source = atoi(argv[2]);
int destination = atoi(argv[3]);
int cost = atoi(argv[4]);
string line;
ifstream myfile(argv[1]);
int i = 0;
if (myfile.is_open()){
while (getline(myfile, line)){
if (i == 0) {
char *token = strtok(const_cast<char*>(line.c_str()), " ");
V = atoi(token);
token = strtok(NULL, " ");
E = atoi(token);
}
else
{
int j = 0;
int s, e, c, dur;
char *token = strtok(const_cast<char*>(line.c_str()), " ");
while (token!=NULL)
{
if (j == 0)
s = atoi(token);
else if(j == 1)
e = atoi(token);
else if(j == 2)
c = atoi(token);
else if(j == 3)
dur = atoi(token);
token = strtok(NULL, " ");
j++;
}
Edge ed = { s,e,c,dur };
v.push_back(ed);
}
i++;
}
myfile.close();
}
else cout << "Unable to open file";
/* Let us create the graph given in above example */
struct Graph* graph = createGraph(V, E);
int index = 0;
for (auto it = v.begin(); it != v.end(); it++) {
graph->edge[index].src = it->src;
graph->edge[index].dest = it->dest;
graph->edge[index].cost = it->cost;
graph->edge[index].duration = it->duration;
index++;
}
int duration=0;
BellmanFord(graph, source, destination,cost,duration);
if (duration < cost)
cout << duration << endl;
else
cout << "0" << endl;
system("pause");
return 0;
}
Explanation / Answer
if you are using linux you can use g++ ford.cpp -std=c++11 (or)
#include <iostream>
#include <string>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <fstream>
#include <vector>
using namespace std;
struct Edge
{
int src, dest, cost, duration;
};
// a structure to represent a connected, directed and
// weighted graph
struct Graph
{
// V-> Number of vertices, E-> Number of edges
int V, E;
// graph is represented as an array of edges.
struct Edge *edge;
};
// Creates a graph with V vertices and E edges
struct Graph *createGraph(int V, int E)
{
struct Graph *graph = new Graph;
graph->V = V;
graph->E = E;
graph->edge = new Edge[E];
return graph;
}
// The main function that finds shortest distances from src to
// all other vertices using Bellman-Ford algorithm. The function
// also detects negative weight cycle
void BellmanFord(struct Graph *graph, int src, int dest, int costLimit, int &totalDuration)
{
int V = graph->V;
int E = graph->E;
int *dist = new int[V];
// Step 1: Initialize distances from src to all other vertices
// as INFINITE
for (int i = 0; i < V; i++)
dist[i] = INT_MAX;
dist[src] = graph->edge[src].cost;
// Step 2: Relax all edges |V| - 1 times. A simple shortest
// path from src to any other vertex can have at-most |V| - 1
// edges
for (int i = 1; i <= V - 1; i++)
{
for (int j = 0; j < E; j++)
{
int u = graph->edge[j].src;
int v = graph->edge[j].dest;
int cost = graph->edge[j].cost;
int dur = graph->edge[j].duration;
if (dist[u] != INT_MAX && dist[u] + cost < costLimit)
{
dist[v] = dist[u] + cost;
totalDuration += dur;
}
}
}
//// Step 3: check for negative-weight cycles. The above step
//// guarantees shortest distances if graph doesn't contain
//// negative weight cycle. If we get a shorter path, then there
//// is a cycle.
for (int i = 0; i < E; i++)
{
int u = graph->edge[i].src;
int v = graph->edge[i].dest;
int cost = graph->edge[i].cost;
int dur = graph->edge[i].duration;
if (dist[u] != INT_MAX && dist[u] + cost < dist[v])
printf("Graph contains negative weight cycle");
}
}
// Driver program to test above functions
int main(int argc, char *argv[])
{
if (argc < 5)
{
cout << "Not valid argument";
return 1;
}
int V; // Number of vertices in graph
int E; // Number of edges in graph
vector<Edge> v;
int source = atoi(argv[2]);
int destination = atoi(argv[3]);
int cost = atoi(argv[4]);
string line;
ifstream myfile(argv[1]);
int i = 0;
if (myfile.is_open())
{
while (getline(myfile, line))
{
if (i == 0)
{
char *token = strtok(const_cast<char *>(line.c_str()), " ");
V = atoi(token);
token = strtok(NULL, " ");
E = atoi(token);
}
else
{
int j = 0;
int s, e, c, dur;
char *token = strtok(const_cast<char *>(line.c_str()), " ");
while (token != NULL)
{
if (j == 0)
s = atoi(token);
else if (j == 1)
e = atoi(token);
else if (j == 2)
c = atoi(token);
else if (j == 3)
dur = atoi(token);
token = strtok(NULL, " ");
j++;
}
Edge ed = {s, e, c, dur};
v.push_back(ed);
}
i++;
}
myfile.close();
}
else
cout << "Unable to open file";
/* Let us create the graph given in above example */
struct Graph *graph = createGraph(V, E);
int index = 0;
for (vector<Edge>::iterator it = v.begin(); it != v.end(); it++)
{
graph->edge[index].src = it->src;
graph->edge[index].dest = it->dest;
graph->edge[index].cost = it->cost;
graph->edge[index].duration = it->duration;
index++;
}
int duration = 0;
BellmanFord(graph, source, destination, cost, duration);
if (duration < cost)
cout << duration << endl;
else
cout << "0" << endl;
// system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.