-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.c
60 lines (54 loc) · 1.43 KB
/
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
#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
/* matrix.c: Our project requires the management of matrix and vector
data structures. This source file provides the implementations of the
necessary utilities, which are declared in matrix.h.
Author: Jason Franklin. */
/* matrix_create: returns a pointer to a Matrix with r rows and c cols */
Matrix matrix_create(int r, int c)
{
Matrix result = (Matrix) malloc(r * sizeof(Vector));
if (result == NULL) {
printf("ERROR: Out of memory\n");
exit(1);
}
int i;
for (i = 0; i < r; i++) {
result[i] = (Vector) malloc(c * sizeof(int));
if (result[i] == NULL) {
printf("ERROR: Out of memory\n");
exit(1);
}
}
return result;
}
/* vector_create: returns a pointer to a Vector of length l */
Vector vector_create(int l)
{
return (Vector) malloc(l * sizeof(int));
}
/* matrix_destroy: de-allocate heap space occupied by m */
void matrix_destroy(Matrix m, int r)
{
int i;
for (i = 0; i < r; i++)
free(m[i]);
free(m);
}
/* vector_destroy: de-allocate heap space occupied by v */
void vector_destroy(Vector v)
{
free(v);
}
/* vector_lte: return 1 if v1 <= v2, 0 if not */
int vector_lte(Vector a, Vector b, int len)
{
int i;
for (i = 0; i < len; i++)
if (a[i] > b[i]) break;
if (i == len)
return 1;
else
return 0;
}