-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrixLib.c
More file actions
122 lines (97 loc) · 1.94 KB
/
Copy pathmatrixLib.c
File metadata and controls
122 lines (97 loc) · 1.94 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include"matrixLib.h"
#define p 0.4
#define MAX_VALUE 15
#define MAX_DISTANCE 99
int **createMat(int n){
int **matA = malloc(sizeof(int*)*n);
if(matA == NULL){
fprintf(stderr, "Error in malloc\n");
exit(EXIT_FAILURE);
}
for(int i = 0; i<n; i++){
matA[i] = malloc(sizeof(int)*n);
if(matA[i] == NULL){
fprintf(stderr, "Error in malloc\n");
exit(EXIT_FAILURE);
}
for(int j= 0; j<n; j++){
matA[i][j] = 0;
}
}
return matA;
}
void populateMatAdj(int **matA, int n){
srand(time(NULL));
for(int i = 0; i<n; i++){
for(int j = 0; j<=i; j++){
if( (double)rand()/RAND_MAX > p){
matA[i][j] = 1;
matA[j][i] = 1;
}
}
}
}
void visualizeMat(int **matA, int n){
for(int i = 0; i<n; i++){
printf("\n");
for(int j = 0; j<n; j++){
printf("%d ", matA[i][j]);
}
}
printf("\n\n");
}
int **matFile(char *name){
int n;
FILE *f = fopen(name, "r+");
if(f == NULL){
fprintf(stderr, "Error in opening file\n");
exit(EXIT_FAILURE);
}
fscanf(f,"%d\n",&n);
int **matA = createMat(n);
for(int i = 0; i<n; i++){
for(int j =0; j<n; j++){
fscanf(f, "%d", &matA[i][j]);
}
fscanf(f, "\n");
}
return matA;
}
int takeNum(char *name){
int n;
FILE *f = fopen(name, "r+");
if(f == NULL){
fprintf(stderr, "Error in opening file\n");
exit(EXIT_FAILURE);
}
fscanf(f, "%d", &n);
return n;
}
int **disMat(int **myAdj, int n){
int l;
int **myDis = createMat(n);
for(int i=0; i<n; i++){
for(int j= 0; j<=i; j++){
if(myAdj[i][j] == 1){
l = (int)((double)rand()/RAND_MAX*MAX_VALUE);
myDis[i][j] = l;
myDis[j][i] = l;
}
}
}
return myDis;
}
bool controlValues(int *visited, int n){
for(int i=0; i<n; i++){
if(visited[i] == 0) return false;
}
return true;
}
void initMatAdj(int *ptn, int ***ptmyAdj, char *filename){
*ptn = takeNum(filename);
*ptmyAdj = matFile(filename);
}