-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_add.c
78 lines (64 loc) · 2.07 KB
/
matrix_add.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
#include<stdio.h>
void main()
{
int array1[20][20], array2[20][20];
int r1, r2, c1, c2, sum[20][20], i, j;
printf("Type the number of rows and columns of first matrix respectively");
scanf("%d %d",&r1,&c1);
printf("Type the number of rows and columns of second matrix respectively");
scanf("%d %d",&r2,&c2);
if(r1!=r2 || c1!=c2)
printf("Matrix additon not possible");
else
{
// For inputting values in 1st mat
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("Element for 1st matrix at[%d][%d]",i,j);
scanf("%d",&array1[i][j]);
}
}
// For printing values of 1st mat
printf("1st matrix is \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("%d \t",array1[i][j]);
}
printf("\n");
}
//For inputting values in 2nd mat
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("Element for 2nd matrix at[%d][%d]",i,j);
scanf("%d",&array2[i][j]);
}
}
// For printing values of 2nd mat
printf("2nd matrix is \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("%d \t",array2[i][j]);
}
printf("\n");
}
// For adding two matrices
printf("The addition of matrix is \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
sum[i][j]=array1[i][j]+array2[i][j];
printf("%d \t",sum[i][j]);
}
printf("\n");
}
}
}