-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ5.c
47 lines (39 loc) · 1.13 KB
/
Q5.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
// 5. Write a C program to add two matrix using pointers.
#include<stdio.h>
void addMatrix(int *a, int *b, int *c, int n) {
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
*(c + i * n + j) = *(a + i * n + j) + *(b + i * n + j);
}
}
}
void printMatrix(int *matrix, int n) {
printf("Resultant Matrix (A + B):\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
printf("%3d ", *(matrix + i * n + j*n));
}
printf("\n");
}
}
int main() {
int n;
int a[10][10], b[10][10], c[10][10];
printf("Enter the size of the matrices (n x n): ");
scanf("%d", &n);
printf("Enter elements of the 1st matrix:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of the 2nd matrix:\n");
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
addMatrix((int *)a, (int *)b, (int *)c, n);
printMatrix((int *)c,n);
return 0;
}