-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_MatrixAddition.c
52 lines (45 loc) · 1.38 KB
/
3_MatrixAddition.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
/*
3. Write a program to add two matrices.
*/
#include <stdio.h>
#define ROW 3
#define COL 3
void matrixInput(int mat[][COL]); // Function to input elements in matrix
void matrixPrint(int mat[][COL]); // Function to print elements of matrix
void matrixAdd(int mat1[][COL], int mat2[][COL], int res[][COL]); // Function to add two matrices
int main() {
int mat1[ROW][COL], mat2[ROW][COL], res[ROW][COL];
printf("Enter elements in first matrix of size %dx%d: \n", ROW, COL);
matrixInput(mat1);
printf("\nEnter elements in second matrix of size %dx%d: \n", ROW, COL);
matrixInput(mat2);
matrixAdd(mat1, mat2, res);
printf("\nSum of first and second matrix: \n");
matrixPrint(res);
return 0;
}
void matrixInput(int mat[][COL]) {
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
scanf("%d", (*(mat + i) + j));
}
}
}
void matrixPrint(int mat[][COL]) {
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
printf("%d ", *(*(mat + i) + j));
}
printf("\n");
}
}
void matrixAdd(int mat1[][COL], int mat2[][COL], int res[][COL]){
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
*(*(res + i) + j) = *(*(mat1 + i) + j) + *(*(mat2 + i) + j);
}
}
}