I need to know why I get an access violation error and if possible, how to fix i
ID: 3555937 • Letter: I
Question
I need to know why I get an access violation error and if possible, how to fix it. Here is my code:
#include <iostream>
using namespace std;
struct Edge
{
int vertex1;
int vertex2;
};
int adjMat[4][4];
void insertEdge( Edge e, int adjMat[4][4]);
void printGraph (int adjMat[4][4]);
struct Node
{
int Vertex;
Node* next;
};
void printList(Node* adjList[4][4]);
int main()
{
Edge one; Edge two; Edge three; Edge four; Edge five; Edge six;
one.vertex1 = 0;
one.vertex2 = 1;
two.vertex1 = 0;
two.vertex2 = 3;
three.vertex1 = 1;
three.vertex2 = 0;
four.vertex1 =1;
four.vertex2 =2;
five.vertex1 = 2;
five.vertex2 = 1;
six.vertex1 = 3;
six.vertex2 = 0;
insertEdge(one, adjMat);
insertEdge(two, adjMat);
insertEdge(three, adjMat);
insertEdge(four, adjMat);
insertEdge(five, adjMat);
insertEdge(six, adjMat);
cout<<"Adjacency Matrix ";
cout<<" 1 2 3 4 ";
printGraph(adjMat);
cout << "Adjacency List: ";
Node* adjList[4][4];
adjList[4][4] = new Node;
adjList[0][0] = new Node;
adjList[0][1] = new Node;
adjList[0][2] = new Node;
adjList[0][3] = new Node;
adjList[1][0] = new Node;
adjList[1][1] = new Node;
adjList[1][2] = new Node;
adjList[1][3] = new Node;
adjList[2][0] = new Node;
adjList[2][1] = new Node;
adjList[2][2] = new Node;
adjList[2][3] = new Node;
adjList[3][0] = new Node;
adjList[3][1] = new Node;
adjList[3][2] = new Node;
adjList[3][3] = new Node;
/*adjList[0][1]->Vertex = 1;
adjList[0][3]->Vertex = 1;
adjList[1][0]->Vertex = 2;
adjList[1][2]->Vertex = 2;
adjList[2][1]->Vertex = 3;
adjList[3][0]->Vertex = 4;*/
adjList[0][0] -> Vertex = NULL;
adjList[0][1] -> Vertex = 1;
adjList[0][2] -> Vertex = NULL;
adjList[0][3] -> Vertex = 1;
adjList[0][0] -> next = adjList[0][1];
adjList[0][0] -> next = NULL;
adjList[0][1] -> next = adjList[0][2];
adjList[0][1] -> next -> Vertex = 2;
adjList[0][2] -> next = adjList[0][3];
adjList[0][2] -> next = NULL;
adjList[0][3] -> next -> Vertex = 4;
adjList[1][0] -> Vertex = 2;
adjList[1][1] -> Vertex = NULL;
adjList[1][2] -> Vertex = 3;
adjList[1][3] -> Vertex = NULL;
adjList[1][0] -> next = adjList[1][1];
adjList[1][0] -> next -> Vertex = 3;
adjList[1][1] -> next = adjList[1][2];
adjList[1][1] -> next -> Vertex = 3;
adjList[1][2] -> next = adjList[1][3];
adjList[1][2] -> next -> Vertex = 2;
adjList[1][3] -> next = NULL;
adjList[2][0] -> Vertex = NULL;
adjList[2][1] -> Vertex = 3;
adjList[2][2] -> Vertex = NULL;
adjList[2][3] -> Vertex = NULL;
adjList[2][0] -> next = adjList[2][1];
adjList[2][0] -> next -> Vertex = 2;
adjList[2][1] -> next = adjList[2][2];
adjList[2][1] -> next -> Vertex = 2;
adjList[2][2] -> next = NULL;
adjList[2][3] -> next = NULL;
adjList[3][1] -> Vertex = NULL;
adjList[3][2] -> Vertex = NULL;
adjList[3][3] -> Vertex = NULL;
adjList[3][0] -> next = adjList[3][1];
adjList[3][0] -> next -> Vertex = 1;
adjList[3][1] -> next = adjList[3][2];
adjList[3][1] -> next -> Vertex = NULL;
adjList[3][2] -> next = NULL;
adjList[3][3] -> next = NULL;
printList(adjList);
system("pause");
}
void insertEdge(Edge e, int adjMat[4][4])
{
int a;
int b;
a = e.vertex1;
b = e.vertex2;
adjMat[a][b] = 1;
}
void printGraph(int adjMat[4][4])
{
int x = 0;
int y = 0;
while (x < 4)
{
cout << x + 1 << " [" << adjMat[x][y] << "] ";
while (y < 3)
{
y++;
cout << " [" << adjMat[x][y] << "] ";
}
x++;
y = 0;
cout << endl;
}
}
void printList(Node* adjList[4][4])
{
int a;
for(int b = 0; b < 3; b++)
{
a = 0;
do
{
cout << "(" << adjList[b][a] -> Vertex;
cout << ", " << adjList[b][a] -> next -> Vertex << ") ";
a++;
}
while(adjList[b][a] -> next != NULL);
}
cout << endl << endl;
}
Explanation / Answer
#include using namespace std; struct Edge { int vertex1; int vertex2; }; int adjMat[4][4]; void insertEdge( Edge e, int adjMat[4][4]); void printGraph (int adjMat[4][4]); struct Node { int Vertex; Node* next; }; void printList(Node* adjList[4][4]); int main() { Edge one; Edge two; Edge three; Edge four; Edge five; Edge six; one.vertex1 = 0; one.vertex2 = 1; two.vertex1 = 0; two.vertex2 = 3; three.vertex1 = 1; three.vertex2 = 0; four.vertex1 =1; four.vertex2 =2; five.vertex1 = 2; five.vertex2 = 1; six.vertex1 = 3; six.vertex2 = 0; insertEdge(one, adjMat); insertEdge(two, adjMat); insertEdge(three, adjMat); insertEdge(four, adjMat); insertEdge(five, adjMat); insertEdge(six, adjMat); coutVertex = 3; adjList[3][0]->Vertex = 4;*/ adjList[0][0] -> Vertex = NULL; adjList[0][1] -> Vertex = 1; adjList[0][2] -> Vertex = NULL; adjList[0][3] -> Vertex = 1; adjList[0][0] -> next = adjList[0][1]; adjList[0][0] -> next = NULL; adjList[0][1] -> next = adjList[0][2]; adjList[0][1] -> next -> Vertex = 2; adjList[0][2] -> next = adjList[0][3]; adjList[0][2] -> next = NULL; adjList[0][3]->next=adjList[0][2]; adjList[0][3] -> next -> Vertex = 4; //Here adjlist[0][3]->next was null and you were trying to access vertex of it adjList[1][0] -> Vertex = 2; adjList[1][1] -> Vertex = NULL; adjList[1][2] -> Vertex = 3; adjList[1][3] -> Vertex = NULL; adjList[1][0] -> next = adjList[1][1]; adjList[1][0] -> next -> Vertex = 3; adjList[1][1] -> next = adjList[1][2]; adjList[1][1] -> next -> Vertex = 3; adjList[1][2] -> next = adjList[1][3]; adjList[1][2] -> next -> Vertex = 2; adjList[1][3] -> next = NULL; adjList[2][0] -> Vertex = NULL; adjList[2][1] -> Vertex = 3; adjList[2][2] -> Vertex = NULL; adjList[2][3] -> Vertex = NULL; adjList[2][0] -> next = adjList[2][1]; adjList[2][0] -> next -> Vertex = 2; adjList[2][1] -> next = adjList[2][2]; adjList[2][1] -> next -> Vertex = 2; adjList[2][2] -> next = NULL; adjList[2][3] -> next = NULL; adjList[3][1] -> Vertex = NULL; adjList[3][2] -> Vertex = NULL; adjList[3][3] -> Vertex = NULL; adjList[3][0] -> next = adjList[3][1]; adjList[3][0] -> next -> Vertex = 1; adjList[3][1] -> next = adjList[3][2]; adjList[3][1] -> next -> Vertex = NULL; adjList[3][2] -> next = NULL; adjList[3][3] -> next = NULL; printList(adjList); system("pause"); } void insertEdge(Edge e, int adjMat[4][4]) { int a; int b; a = e.vertex1; b = e.vertex2; adjMat[a][b] = 1; } void printGraph(int adjMat[4][4]) { int x = 0; int y = 0; while (x < 4) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.