-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu-driven-matrix.c
181 lines (157 loc) · 5.79 KB
/
menu-driven-matrix.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
// input a matrix
void inputMatrix(int matrix[10][10], int rows, int columns) {
printf("Enter elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}
// display a matrix
void displayMatrix(int matrix[10][10], int rows, int columns) {
printf("Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
// add two matrices
void addMatrices(int matrix1[10][10], int matrix2[10][10], int result[10][10], int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
// subtract two matrices
void subtractMatrices(int matrix1[10][10], int matrix2[10][10], int result[10][10], int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}
// upper triangular matrix
void upperTriangular(int matrix[10][10], int rows, int columns) {
printf("Upper Triangular Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (j >= i) {
printf("%d\t", matrix[i][j]);
} else {
printf("0\t");
}
}
printf("\n");
}
}
// lower triangular matrix
void lowerTriangular(int matrix[10][10], int rows, int columns) {
printf("Lower Triangular Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
if (j <= i) {
printf("%d\t", matrix[i][j]);
} else {
printf("0\t");
}
}
printf("\n");
}
}
// transpose of a matrix
void transposeMatrix(int matrix[10][10], int result[10][10], int rows, int columns) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
result[j][i] = matrix[i][j];
}
}
}
// multiply two matrices
void multiplyMatrices(int matrix1[10][10], int matrix2[10][10], int result[10][10], int rows1, int columns1, int rows2, int columns2) {
if (columns1 != rows2) {
printf("Number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication.\n");
return;
}
printf("Product of Matrices:\n");
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < columns2; j++) {
result[i][j] = 0;
for (int k = 0; k < columns1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
printf("%d\t", result[i][j]);
}
printf("\n");
}
}
int main() {
int choice, rows1, columns1, rows2, columns2;
int matrix1[10][10], matrix2[10][10], result[10][10];
printf("Enter the number of rows and columns for matrix 1:\n");
scanf("%d %d", &rows1, &columns1);
inputMatrix(matrix1, rows1, columns1);
displayMatrix(matrix1, rows1, columns1);
printf("\nEnter the number of rows and columns for matrix 2:\n");
scanf("%d %d", &rows2, &columns2);
inputMatrix(matrix2, rows2, columns2);
displayMatrix(matrix2, rows2, columns2);
do {
printf("\nMenu:\n");
printf("1. Addition of two matrices\n");
printf("2. Subtraction of two matrices\n");
printf("3. Find upper triangular matrix\n");
printf("4. Find lower triangular matrix\n");
printf("5. Transpose of a matrix\n");
printf("6. Product of two matrices\n");
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
if (rows1 == rows2 && columns1 == columns2) {
addMatrices(matrix1, matrix2, result, rows1, columns1);
displayMatrix(result, rows1, columns1);
} else {
printf("Matrices must have the same dimensions for addition.\n");
}
break;
case 2:
if (rows1 == rows2 && columns1 == columns2) {
subtractMatrices(matrix1, matrix2, result, rows1, columns1);
displayMatrix(result, rows1, columns1);
} else {
printf("Matrices must have the same dimensions for subtraction.\n");
}
break;
case 3:
upperTriangular(matrix1, rows1, columns1);
break;
case 4:
lowerTriangular(matrix1, rows1, columns1);
break;
case 5:
transposeMatrix(matrix1, result, rows1, columns1);
displayMatrix(result, columns1, rows1);
break;
case 6:
if (columns1 == rows2) {
multiplyMatrices(matrix1, matrix2, result, rows1, columns1, rows2, columns2);
// displayMatrix(result, rows1, columns2);
} else {
printf("Number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication.\n");
}
break;
case 0:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 0);
return 0;
}