-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
102 lines (82 loc) · 1.82 KB
/
functions.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
100
101
102
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "functions.h"
#include "linked_list.h"
int **getAdjacencyMatrixArray (const char* fileName)
{
FILE* file = fopen (fileName, "r");
// In case of an error
if(file == NULL)
{
printf("File does not exist...\n");
exit(0);
}
int id, degree;
int start, finish;
int s1, s2, edges;
int i, j, row;
// Scan the first line
if (fscanf(file, "%d %d %d", &s1, &s2, &edges) != 3)
{
printf("Failed to read file");
}
// Initialise the array
int **arr = (int **)malloc(s1 * sizeof(int *));
for (row = 0; row < s2; row++)
{
arr[row] = (int *)malloc(s2 * sizeof(int));
}
// Fill the array with zeros
for(i = 0; i < s1; i++)
{
for(j = 0; j < s2; j++)
{
arr[i][j] = 0;
}
}
// Read the file and update the values
while (!feof(file))
{
if (fscanf(file, "%d %d", &start, &finish) != 2)
{
printf("Failed to read file");
}
arr[start-1][finish-1] = 1;
}
fclose (file);
return arr;
}
int getAdjacencyMatrixSize(const char* fileName)
{
FILE* file = fopen (fileName, "r");
// In case of an error
if(file == NULL)
{
printf("File does not exist...\n");
exit(0);
}
int s1;
// Scan the first number
if (fscanf(file, "%d", &s1) != 1)
{
printf("Failed to read file");
}
return s1;
}
int *initialIndegree(int **arr, int size)
{
int i, j, sum;
int *result = (int*)malloc(size * sizeof(int));
// Sum of every row
for (i = 0; i < size; i++)
{
sum = 0;
for (j = 0; j < size; j++)
{
sum += arr[j][i];
}
result[i] = sum;
}
return result;
}