-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeAlpha_MatrixOperations.c
More file actions
129 lines (101 loc) · 3.01 KB
/
Copy pathCodeAlpha_MatrixOperations.c
File metadata and controls
129 lines (101 loc) · 3.01 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdio.h>
void inputMatrix(int rows, int cols, int matrix[10][10]) {
int i, j;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}
}
void displayMatrix(int rows, int cols, int matrix[10][10]) {
int i, j;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
void addMatrices(int rows, int cols, int A[10][10], int B[10][10]) {
int C[10][10];
int i, j;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
printf("\nResultant Matrix:\n");
displayMatrix(rows, cols, C);
}
void multiplyMatrices(int r1, int c1, int r2, int c2,
int A[10][10], int B[10][10]) {
int C[10][10];
int i, j, k;
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
C[i][j] = 0;
for(k = 0; k < c1; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
printf("\nResultant Matrix:\n");
displayMatrix(r1, c2, C);
}
void transposeMatrix(int rows, int cols, int A[10][10]) {
int T[10][10];
int i, j;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
T[j][i] = A[i][j];
}
}
printf("\nTranspose Matrix:\n");
displayMatrix(cols, rows, T);
}
int main() {
int choice;
int A[10][10], B[10][10];
int r1, c1, r2, c2;
printf("1. Matrix Addition\n");
printf("2. Matrix Multiplication\n");
printf("3. Matrix Transpose\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter rows and columns: ");
scanf("%d%d", &r1, &c1);
printf("Enter Matrix A:\n");
inputMatrix(r1, c1, A);
printf("Enter Matrix B:\n");
inputMatrix(r1, c1, B);
addMatrices(r1, c1, A, B);
break;
case 2:
printf("Enter rows and columns of Matrix A: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns of Matrix B: ");
scanf("%d%d", &r2, &c2);
if(c1 != r2) {
printf("Matrix multiplication not possible.\n");
} else {
printf("Enter Matrix A:\n");
inputMatrix(r1, c1, A);
printf("Enter Matrix B:\n");
inputMatrix(r2, c2, B);
multiplyMatrices(r1, c1, r2, c2, A, B);
}
break;
case 3:
printf("Enter rows and columns: ");
scanf("%d%d", &r1, &c1);
printf("Enter Matrix:\n");
inputMatrix(r1, c1, A);
transposeMatrix(r1, c1, A);
break;
default:
printf("Invalid choice!");
}
return 0;
}