Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly. Hello there. I am trying to implement the djikstras shortest path algorithm into my code in C++ with a vector of vectors. However, when I test I get an error "Exception thrown at 0x75FFC762 in graphMain.exe: Microsoft C++ exception: std::out_of_range at memory location 0x009CF26C" for line 115 in graph.cpp. Could you help me debug? I am so close!
----------------------------FILE NAME:
pch.h---------------------------------------
// pch.cpp: source file corresponding to pre-compiled header;
necessary for compilation to succeed
#include "pch.h"
// In general, ignore this file, but keep it around if you are using pre-compiled headers.
---------------------------FILE NAME:
graph.h---------------------------------------
#include "pch.h"
#include <vector>
using std::vector;
using std::string;
struct Edge
{
int dest, weight = 0;
};
class chart
{
public:
//the adjacency list
vector <vector<Edge*>*> list;
chart();
void addNode();
void addPath(int source, int dest, int weight);
void printGraph();
int minDist(vector <int> distance, vector
<bool> visited);
void dijkstra(vector <vector<Edge*>*>
list, int source);
void printPath(vector <int> distance, int
size);
};
-------------------------------FILE NAME:
graph.cpp-------------------------------
#include "pch.h"
#include <iostream>
#include "graph.h"
#include <vector>
using std::cout;
using std::endl;
using std::vector;
chart::chart()
{
}
void chart::addNode()
{
//creating a new node and adding it to the 2D Vector
of edges
vector <Edge*> *edges = new vector<Edge
*>;
list.push_back(edges);
}
void chart::addPath(int source, int dest, int weight)
{
//creating the edge
Edge *edge = new Edge;
//filling the edge with a destination and weight
edge->dest = dest;
edge->weight = weight;
//adding an edge to the graph
for (unsigned int i = 0; i < list.size();
++i)
{
//goes back to the main menu if the
user tries to add to a location that doesnt exist
if (source > list.size() - 1 ||
dest > list.size() - 1 || source < 0)
{
cout <<
"The graph isn't that big yet!" << endl;
return;
}
}
//list.at(source)->resize(1);
//trying to push into the inner vector using the
location input by the user
list.at(source)->push_back(edge);
//list.at(source)->at(0)->dest = dest;
//list.at(source)->at(0)->weight = weight;
}
void chart::printGraph()
{
int count = 0;
for (unsigned int i = 0; i < list.size();
++i)
{
if (list[i]->size() == 0)
{
cout <<
"Node: " << count << endl;
cout <<
"No paths defined." << endl;
cout <<
endl;
}
else
{
for (unsigned
int j = 0; j < list[i]->size(); ++j)
{
cout << "Node: " << count <<
endl;
cout << "Destination: " <<
list.at(i)->at(j)->dest << endl;
cout << "Length: " <<
list.at(i)->at(j)->weight << endl;
cout << endl;
}
}
count++;
}
}
int chart::minDist(vector <int> distance, vector <bool>
visited)
{
int min = INT_MAX, index = -1;
for (int i = 0; i < list.size(); i++)
{
if (visited[i] == false &&
distance[i] <= min)
{
min =
distance[i];
index = i;
}
}
return index;
}
void chart::dijkstra(vector <vector<Edge*>*> list,
int source)
{
vector <int> distance(list.size()); //holding
the distances from source vertex
vector <bool> visited(list.size()); //holds
checked/unchecked paths
for (int i = 0; i < list.size(); i++)
{
distance[i] = INT_MAX;
visited[i] = false;
}
distance[source] = 0;
for (int i = 0; i < list.size(); i++)
{
int u = minDist(distance,
visited);
visited[u] = true;
for (int j = 0; j <
list[i]->size(); ++j)
{
if (!visited[j]
&& list.at(u)->at(j) != 0 && distance[u] !=
INT_MAX && distance[u] + list.at(u)->at(j)->weight
< distance[j])
{
distance[j] = distance[u] +
list.at(u)->at(j)->weight;
}
}
}
printPath(distance, list.size());
}
void chart::printPath(vector <int> distance, int
size)
{
cout << "Distance from source to destination
is:\n " << endl;
for (int i = 0; i < size; i++)
{
cout << i << "----"
<< distance[i] << endl;
}
cout << endl;
}
------------------------------FILE NAME:
graphMain.cpp-----------------------------
// graphMain.cpp : This file contains the 'main' function. Program
execution begins and ends there.
//
#include "pch.h"
#include "graph.h"
#include <iostream>
#include <string>
#include <vector>
using std::endl;
using std::cout;
using std::cin;
void testFunc()
{
chart g;
g.addNode();
g.addNode();
g.addNode();
g.addNode();
g.addNode();
g.addPath(0, 3, 10);
g.addPath(0, 1, 3);
g.addPath(1, 2, 4);
g.addPath(2, 3, 4);
cout << "Printing the graph...\n" <<
endl;
g.printGraph();
g.dijkstra(g.list, 0);
g.dijkstra(g.list, 1);
g.dijkstra(g.list, 2);
g.dijkstra(g.list, 3);
g.dijkstra(g.list, 4);
}
int main()
{
testFunc();
chart g;
int count, choice;
choice = -1;
count = 0;
while (choice != 5)
{
int source, dest;
cout << "Main menu. Your
choices are as follows:" << endl;
cout << "1. Add a node"
<< endl;
cout << "2. Add a path"
<< endl;
cout << "3. Print the graph"
<< endl;
cout << "4. Find the shortest
path" << endl;
cout << "5. Quit" <<
endl;
cin >> choice;
switch (choice)
{
case 1:
g.addNode();
cout <<
"You've added node " << count << " to the graph."
<< endl;
count++;
break;
case 2:
int
length;
cout <<
"What do you wanna add? (source, dest, distance)" <<
endl;
cout <<
"Source?" << endl;
cin >>
source;
cout <<
"Destination?" << endl;
cin >>
dest;
cout <<
"Distance?" << endl;
cin >>
length;
g.addPath(source, dest, length);
break;
case 3:
cout <<
"The graph contains: " << endl;
g.printGraph();
break;
case 4:
cout <<
"Which node would you like to use as a source?" <<
endl;
cin >>
source;
g.dijkstra(g.list, source);
break;
case 5:
return 0;
break;
}
}
}
The issue is happening in the following line
if (!visited[j] && list.at(u)->at(j) != 0 && distance[u] != INT_MAX && distance[u] + list.at(u)->at(j)->weight < distance[j])
because the inner vector size is less than the one we are trying to access using at operator.
Attach the Resolved Solution in some time.
Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly....
Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...
15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){ for(int i=1; i<=x; i++){ cout<<x; } } int main(){ int x,i; cin >> x; count(x); return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...
The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...
I am getting a seg fault with my huffman tree. I'm not sure if it's my deconstructors but also getting some memory leaks. Can some one help me find my seg fault? It's in c++, thanks! //#ifndef HUFFMAN_HPP //#define HUFFMAN_HPP #include<queue> #include<vector> #include<algorithm> #include<iostream> #include <string> #include <iostream> using namespace std; class Node { public: // constructor with left and right NULL nodes Node(char charTemp, int c) { ch = charTemp; count = c; left...
Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...
Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...
fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str; while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title); getline(inFile, books[size].Author); getline(inFile, books[size].publisher); getline(inFile,...
Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...
I need to add something to this C++ program.Additionally I want it to remove 10 words from the printing list (Ancient,Europe,Asia,America,North,South,West ,East,Arctica,Greenland) #include <iostream> #include <map> #include <string> #include <cctype> #include <fstream> #include <iomanip> using namespace std; void addWord(map<std::string,int> &words,string s); void readFile(string infile,map<std::string,int> &words); void display(map<std::string,int> words); int main() { // Declaring variables std::map<std::string,int> words; //defines an input stream for the data file ifstream dataIn; string infile; cout<<"Please enter a File Name :"; cin>>infile; readFile(infile,words);...
Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() { int n; double avg, sum = 0;; string in_file, out_file; cout << "Please enter the name of the input file: "; cin >> in_file; ...