How to construct a adj-matrix

#include<stdio.h>
#include<string.h>
#include <stdlib.h>
# define MaxVNum 100

using namespace std;
// REF: https://www.youtube.com/watch?v=xfL84Kn72nA
typedef struct e {
int from;
int to;
float weight;
} edge;
edge edges[256], *Edge;
int numberOfEdges = 0;
//Edge E;
//E = (Edge)malloc(sizeof(edge));

typedef struct v {
int v;
}vertex;
vertex vertexes[256];
int numberOfV = 0;

void readMap(){
FILE * map;
map = fopen("map.txt", "r");
while (!feof(map)) {
fscanf(map, "%d %d %f", &edges[numberOfEdges].from, &edges[numberOfEdges].to, &edges[numberOfEdges].weight);
numberOfEdges++;
}
fclose(map);
}

void printEdges() {
for (int i = 0; i < numberOfEdges; i++) {
printf("%d %d %f\n", edges[i].from, edges[i].to, edges[i].weight);
}
}

unsigned int exist(int s, vertex a[]) {
unsigned int exist = 0;
for (int i = 0; i < numberOfV; i++) {
if (s - a[i].v == 0) {
exist = 1;
}
}
return exist;
}

void readVertex() {
for (int i = 0; i < numberOfEdges; i++) {
if (!exist(edges[i].from, vertexes)) {
vertexes[numberOfV].v = edges[i].from;
numberOfV++;
}
if (!exist(edges[i].to, vertexes)) {
vertexes[numberOfV].v = edges[i].to;
numberOfV++;
}
}
}

void printVertex() {
printf("\n Our Vertex: ");
for (int i = 0; i < numberOfV; i++) {
printf("%d\t", vertexes[i].v);
}
}

typedef struct graph {
int Nv;
int Ne;
int matrix[MaxVNum][MaxVNum];
}graph,*MyGraph;
graph g;

//initialize teh graph that has numebr of V vertex
void InitGraph(MyGraph G) {
G->Nv = numberOfV;
G->Ne = 0;
//initialize the weight of each edge
for (int i = 0; i < numberOfV; i++) {
for (int j = 0; j < numberOfV; j++) {
G->matrix[i][j] = 0;
}
}
}
//insert the weight
void insertEdge(MyGraph G) {
for (int i = 0; i < numberOfEdges; i++) {

G->matrix[Edge->from][Edge->to] = Edge->weight;
G->matrix[Edge->to][Edge->from] = Edge->weight;
}
}
void printGraph() {
for (int j = 0; j < numberOfV; j++)
{
for (int i = 0; i < numberOfV; i++){
printf("%5d", g.matrix[i][j]);
printf("\n");
}
}
}

int main() {
readMap();
printEdges();
readVertex();
printVertex();
MyGraph G;
G = (MyGraph)malloc(sizeof(graph));
InitGraph(G);
insertEdge(G);
printGraph();
}
Terminal:Segmentation fault (core dumped)
I know there are some errors when constructing the matrix. How to fix it? Thanks for your kindly help.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Lines 26-27: Do not loop on (! feof()). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record.

As formatted:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MaxVNum 100

typedef struct e {
	int from;
	int to;
	float weight;
} edge;

edge edges[256], *Edge;
int numberOfEdges = 0;

//Edge E;
//E = (Edge)malloc(sizeof(edge));

typedef struct v {
	int v;
} vertex;

vertex vertexes[256];
int numberOfV = 0;

void readMap() {
	FILE* map = fopen("map.txt", "r");

	while (!feof(map)) {
		fscanf(map, "%d %d %f", &edges[numberOfEdges].from, &edges[numberOfEdges].to, &edges[numberOfEdges].weight);
		numberOfEdges++;
	}

	fclose(map);
}

void printEdges() {
	for (int i = 0; i < numberOfEdges; i++) {
		printf("%d %d %f\n", edges[i].from, edges[i].to, edges[i].weight);
	}
}

unsigned int exist(int s, vertex a[]) {
	unsigned int exist = 0;
	for (int i = 0; i < numberOfV; i++) {
		if (s - a[i].v == 0) {
			exist = 1;
		}
	}

	return exist;
}

void readVertex() {
	for (int i = 0; i < numberOfEdges; i++) {
		if (!exist(edges[i].from, vertexes)) {
			vertexes[numberOfV].v = edges[i].from;
			numberOfV++;
		}

		if (!exist(edges[i].to, vertexes)) {
			vertexes[numberOfV].v = edges[i].to;
			numberOfV++;
		}
	}
}

void printVertex() {
	printf("\n Our Vertex: ");

	for (int i = 0; i < numberOfV; i++) {
		printf("%d\t", vertexes[i].v);
	}
}

typedef struct graph {
	int Nv;
	int Ne;
	int matrix[MaxVNum][MaxVNum];
}graph, * MyGraph;

graph g;

//initialize the graph that has number of V vertex
void InitGraph(MyGraph G) {
	G->Nv = numberOfV;
	G->Ne = 0;

	//initialize the weight of each edge
	for (int i = 0; i < numberOfV; i++) {
		for (int j = 0; j < numberOfV; j++) {
			G->matrix[i][j] = 0;
		}
	}
}

//insert the weight
void insertEdge(MyGraph G) {
	for (int i = 0; i < numberOfEdges; i++) {
		G->matrix[Edge->from][Edge->to] = Edge->weight;
		G->matrix[Edge->to][Edge->from] = Edge->weight;
	}
}

void printGraph() {
	for (int j = 0; j < numberOfV; j++) {
		for (int i = 0; i < numberOfV; i++) {
			printf("%5d", g.matrix[i][j]);
			printf("\n");
		}
	}
}

int main() {
	readMap();
	printEdges();
	readVertex();
	printVertex();

	MyGraph G = (MyGraph)malloc(sizeof(graph));

	InitGraph(G);
	insertEdge(G);
	printGraph();
}



For readMap(), consider (not tried) :

1
2
3
4
5
6
7
8
9
void readMap() {
	FILE* map = fopen("map.txt", "r");

	while (fscanf(map, "%d %d %f", &edges[numberOfEdges].from, &edges[numberOfEdges].to, &edges[numberOfEdges].weight) != EOF) {
		numberOfEdges++;
	}

	fclose(map);
}

Last edited on
Thank you very much!
Topic archived. No new replies allowed.