-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_addition.c
57 lines (54 loc) · 1.14 KB
/
matrix_addition.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
#include<stdio.h>
void add_matrix(int n,int a[n][n],int b[n][n]){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j]=a[i][j]+b[i][j];
}
}
}
void main(){
int n;
printf("enter the number of rows in matrix\n");
//we are assuming it's a n x n matrix
scanf("%d",&n);
int a[n][n];
int b[n][n];
printf("enter the first matrix\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int c;
printf("enter a[%d][%d] ",i,j);
scanf("%d",&c);
a[i][j]=c;
}
}
printf("enter the second matrix\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
int c;
printf("enter b[%d][%d] ",i,j);
scanf("%d",&c);
b[i][j]=c;
}
}printf("first matrix\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf(" %d ",a[i][j]);
}
printf("\n");
}printf("second matrix\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf(" %d ",b[i][j]);
}
printf("\n");
}
add_matrix(3,a,b);
printf("after addition we get\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf(" %d ",a[i][j]);
}
printf("\n");
}
}