-
Notifications
You must be signed in to change notification settings - Fork 0
/
addition_of_matrices.c
75 lines (67 loc) · 1.88 KB
/
addition_of_matrices.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
//sum of two 3x3 matrices
/*NOTE:This program is for 3x3 matrix but you can extend it to matrices of
higher or lower order by changing m and n
AUTHOR:VED VYAS GITHUB:vedcodes2312 */
#include<stdio.h>//preprocessor directive standard i/p o/p header file
#include<stdlib.h>//standard library
int main()
{
system("cls");
int m=3,n=3;
int a[m][n],b[m][n],sum[m][n];
printf("enter elements of first matrix A:\n");
for (int i = 0; i < m; i++) //input elements of the matrix
{
for (int j = 0; j < n; j++)
{
printf("element [%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("enter elements of second matrix B:\n");
for (int i = 0; i < m; i++) //input elements of the matrix
{
for (int j = 0; j < n; j++)
{
printf("element [%d][%d]: ", i, j);
scanf("%d", &b[i][j]);
}
}
printf("\nmatrix A\n"); //printing first matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("[%d][%d]=%d\t ",i,j, a[i][j]);
}
printf("\n");
}
printf("\nmatrix B\n"); //printing the second matrix
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("[%d][%d]=%d\t ",i,j, b[i][j]);
}
printf("\n");
}
//addition of matrices A and B
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
sum[i][j]= a[i][j]+ b[i][j];
}
}
//printing the result matrix after summation
printf("sum of matrices A and B that is A+B\n");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
printf("[%d][%d]=%d\t ",i,j, sum[i][j]);
}
printf("\n");
}
return 0;
} //end of program