-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathDiagonal_Matrix.c
45 lines (40 loc) · 953 Bytes
/
Diagonal_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
#include <stdio.h>
#include <stdlib.h> // for malloc
// DIAGONAL MATRIX IN C
struct Matrix {
int *A; // This will point to a 1D array in heap
int n; // Diagonal Matrices are square, so only one value needed.
};
void Set(struct Matrix *m, int i, int j, int x) {
if (i == j)
m->A[i-1] = x;
}
int Get(struct Matrix m, int i, int j) {
if(i == j)
return m.A[i-1];
else
return 0;
}
void Display(struct Matrix m) {
for (int i = 0; i < m.n; i++) {
for (int j = 0; j < m.n; j++) {
if (i == j) printf("%d ", m.A[i]);
else printf("0 ");
}
printf("\n");
}
}
int main() {
struct Matrix m;
m.n = 4;
m.A = (int *)malloc(m.n*sizeof(int));
Set(&m,1,1,1);
Set(&m,2,2,9);
Set(&m,3,3,9);
Set(&m,4,4,4);
printf("Value from Get function: %d\n", Get(m,3,3));
Display(m);
free(m.A);
return 0;
}
//Code submitted by @AmanDekate1