-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_array.c
71 lines (68 loc) · 1.21 KB
/
matrix_array.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
// Copyright Mantu Matei 312CA 2022-2023
#include <stdio.h>
#include <stdlib.h>
#define cc const char
#define ERR "Error allocating memory\n"
void free_matrices(int ***matrices, int *m, int *n, int size)
{
int i, j;
for (i = 0; i < size; ++i)
for (j = 0; j < m[i]; ++j)
free(matrices[i][j]);
for (i = 0; i < size; ++i)
free(matrices[i]);
free(matrices);
free(m);
free(n);
}
void init(int ****matrices, int **m, int **n)
{
*m = (int *)malloc(sizeof(int));
if (!*m) {
printf(ERR);
return;
}
*n = (int *)malloc(sizeof(int));
if (!*n) {
free(*m);
printf(ERR);
return;
}
*matrices = (int ***)malloc(sizeof(int **));
if (!*matrices) {
free(*m);
free(*n);
printf(ERR);
return;
}
}
void change_cap(int ****matrices, int **m, int **n, int *cap, cc mode)
{
if (mode == 'I') {
*cap *= 2;
} else if (mode == 'D') {
*cap /= 2;
if (*cap == 0) {
*cap += 1;
return;
}
}
*m = (int *)realloc(*m, *cap * sizeof(int));
if (!*m) {
printf(ERR);
return;
}
*n = (int *)realloc(*n, *cap * sizeof(int));
if (!*n) {
printf(ERR);
free(*m);
return;
}
*matrices = (int ***)realloc(*matrices, *cap * sizeof(int **));
if (!*matrices) {
printf(ERR);
free(*m);
free(*n);
return;
}
}