-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddMatrix.c
78 lines (61 loc) · 2.24 KB
/
AddMatrix.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
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
void displayMatrix(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols);
void addMatrices(int matrix1[MAX_ROWS][MAX_COLS], int matrix2[MAX_ROWS][MAX_COLS], int result[MAX_ROWS][MAX_COLS], int rows, int cols);
int main() {
int rows, cols;
// Input the size of the matrices
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
if (rows > MAX_ROWS || cols > MAX_COLS) {
printf("Error: Matrix size exceeds the maximum allowed size.\n");
return 1; // Exit with an error code
}
int matrix1[MAX_ROWS][MAX_COLS], matrix2[MAX_ROWS][MAX_COLS];
// Input the elements of the first matrix
printf("Enter the elements of the first matrix:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrix1[i][j]);
}
}
// Input the elements of the second matrix
printf("Enter the elements of the second matrix:\n");
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrix2[i][j]);
}
}
// Display the original matrices
printf("\nFirst Matrix:\n");
displayMatrix(matrix1, rows, cols);
printf("\nSecond Matrix:\n");
displayMatrix(matrix2, rows, cols);
// Add the matrices
int result[MAX_ROWS][MAX_COLS];
addMatrices(matrix1, matrix2, result, rows, cols);
// Display the result matrix
printf("\nSum Matrix:\n");
displayMatrix(result, rows, cols);
return 0;
}
void displayMatrix(int matrix[MAX_ROWS][MAX_COLS], int rows, int cols) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
void addMatrices(int matrix1[MAX_ROWS][MAX_COLS], int matrix2[MAX_ROWS][MAX_COLS], int result[MAX_ROWS][MAX_COLS], int rows, int cols) {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}