-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntegerMatrixOperations.c
106 lines (93 loc) · 2.36 KB
/
IntegerMatrixOperations.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
103
104
105
106
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef struct
{
int row;
int col;
int **matrix;
} Matrix;
Matrix M_create(int row, int col)
{
Matrix M;
M.row = row;
M.col =col;
M.matrix = (int **) calloc(row, sizeof(int *));
for (int I=0; I<row;I++)
M.matrix[I] = (int *) calloc(col, sizeof(int));
return M;
}
void M_destroy(Matrix M)
{
for (int I=0;I<M.row;I++)
free(M.matrix[I]);
free(M.matrix);
}
Matrix M_multiply(Matrix M1, Matrix M2)
{
if (M1.col == M2.row)
{
Matrix M = M_create(M1.row, M2.col);
int constant = M1.col;
for (int j=0; j<M.row; j++) {
for (int k=0; k<M.col; k++) {
for (int i=0; i<constant; i++) {
M.matrix[j][k] += (M1.matrix[j][i] *
M2.matrix[i][k]);
}
}
}
return M;
} else
printf("Matrices can't be multiplied\n");
}
Matrix M_add(Matrix M1, Matrix M2)
{
if (M1.row == M2.row &&
M1.col == M2.col)
{
int row = M1.row;
int col = M1.col;
Matrix M = M_create(row, col);
for (int j=0; j<row; j++)
for (int k=0; k<col; k++)
M.matrix[j][k] = M1.matrix[j][k] +
M2.matrix[j][k];
return M;
} else
printf("Matrices aren't compatible for addition\n");
}
Matrix M_subtract(Matrix M1, Matrix M2)
{
if (M1.row == M2.row &&
M1.col == M2.col)
{
int row = M1.row;
int col = M1.col;
Matrix M = M_create(row, col);
for (int j=0; j<row; j++)
for (int k=0; k<col; k++)
M.matrix[j][k] = M1.matrix[j][k] -
M2.matrix[j][k];
return M;
} else
printf("Matrices aren't compatible for subtraction\n");
}
int main()
{
Matrix M[3];
for (int I=0; I<3; I++)
M[I] = M_create(2, 2);
for (int i=0; i<3; i++)
for (int j=0; j<M[i].row; j++)
for (int k=0; k<M[i].col; k++)
M[i].matrix[j][k] = k+1;
M[2] = M_multiply(M[0], M[1]);
for (int i=0; i<3; i++)
for (int j=0; j<M[i].row; j++) {
for (int k=0; k<M[i].col; k++)
printf("%d ", M[i].matrix[j][k]);
printf("\n");
}
return 0;
}