-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_adj_matrix.c
99 lines (80 loc) · 2.24 KB
/
gen_adj_matrix.c
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <limits.h>
#define INF INT_MAX/2
#define DEF_MAX_WEIGHT 9
#define GENERATE(i,j,r,w) (i==j) ? 0: ( ( (r = 1 + rand() % (w+1) ) == w+1) ? INF : r )
#define INPUT_ERROR -1
#define SIZE_ERROR -2
#define MALLOC_ERROR -3
void print_matrix(int rows, int cols, int ***matrix);
int main(int argc, char **argv){
if (argc < 3 || argc > 4) {
printf ("Usage: %s <matrix size> <file> optional: <max weight>\n", argv[0]);
fflush(stdout);
exit(INPUT_ERROR);
}
srand(time(0));
FILE *fout;
int **A, *Astorage, random;
long max_weight, size;
size = strtol(argv[1], NULL, 10);
if (size <= 2){
printf ("<matrix size> must be a number at least equal to 3\n");
fflush(stdout);
exit(SIZE_ERROR);
}
if (size == LONG_MAX){
printf ("Matrix size is too big\n");
fflush(stdout);
exit(SIZE_ERROR);
}
if (argv[3])
max_weight = strtol(argv[3], NULL, 10);
else
max_weight = DEF_MAX_WEIGHT;
if (max_weight < 1){
printf ("<max weight> must be a number at least equal to 1\n");
fflush(stdout);
exit(SIZE_ERROR);
}
if (max_weight >= INF){
printf ("Max weight is too big\n");
fflush(stdout);
exit(SIZE_ERROR);
}
Astorage = (int *) malloc(size * size * sizeof(int));
A = (int **) malloc(size * sizeof(int *));
if (!Astorage || !A){
printf ("Not enough memory\n");
fflush(stdout);
exit(MALLOC_ERROR);
}
for (int i = 0; i < size; i++){
A[i] = &Astorage[i*size];
for (int j = 0; j < size; j++)
A[i][j] = GENERATE(i,j,random,max_weight);
}
fout = fopen(argv[2], "wb");
fwrite(&size, sizeof(long), 1, fout);
fwrite(Astorage, sizeof(int), size * size, fout);
fclose(fout);
// Print only small matrices
if (size <= 32)
print_matrix(size, size, &A);
free(Astorage), Astorage = NULL;
free(A), A = NULL;
return 0;
}
void print_matrix(int rows, int cols, int ***matrix){
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
if ((*matrix)[i][j] == INF)
printf("%4s ", "INF");
else
printf("%4d ", (*matrix)[i][j]);
}
printf("\n");
}
}