-
Notifications
You must be signed in to change notification settings - Fork 16
/
4.c
60 lines (49 loc) · 1.2 KB
/
4.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
#include <stdio.h>
void addMatrices(int *mat1, int *mat2, int *result, int rows, int cols)
{
int totalElements = rows * cols;
for (int i = 0; i < totalElements; i++)
{
result[i] = mat1[i] + mat2[i];
}
}
void printMatrix(int *matrix, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%d ", matrix[i * cols + j]);
}
printf("\n");
}
}
int main()
{
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
int mat1[rows][cols];
int mat2[rows][cols];
int result[rows][cols];
printf("Enter elements of the first matrix:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of the second matrix:\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
scanf("%d", &mat2[i][j]);
}
}
addMatrices((int *)mat1, (int *)mat2, (int *)result, rows, cols);
printf("Resultant matrix:\n");
printMatrix((int *)result, rows, cols);
return 0;
}