Skip to content

Commit a058a76

Browse files
Create assignment_3_p4.c
1 parent f49a4bf commit a058a76

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

Assignments/assignment_3_p4.c

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
3+
// Function to multiply two matrices
4+
void multiplyMatrices(int rows1, int cols1, int mat1[rows1][cols1], int rows2, int cols2, int mat2[rows2][cols2], int result[rows1][cols2]) {
5+
for (int i = 0; i < rows1; i++) {
6+
for (int j = 0; j < cols2; j++) {
7+
result[i][j] = 0;
8+
for (int k = 0; k < cols1; k++) {
9+
result[i][j] += mat1[i][k] * mat2[k][j];
10+
}
11+
}
12+
}
13+
}
14+
15+
// Function to display a matrix
16+
void displayMatrix(int rows, int cols, int mat[rows][cols]) {
17+
for (int i = 0; i < rows; i++) {
18+
for (int j = 0; j < cols; j++) {
19+
printf("%d\t", mat[i][j]);
20+
}
21+
printf("\n");
22+
}
23+
}
24+
25+
int main() {
26+
int rows1, cols1, rows2, cols2;
27+
28+
// Input dimensions for the first matrix
29+
printf("Enter the number of rows for the first matrix: ");
30+
scanf("%d", &rows1);
31+
32+
printf("Enter the number of columns for the first matrix: ");
33+
scanf("%d", &cols1);
34+
35+
// Input dimensions for the second matrix
36+
printf("Enter the number of rows for the second matrix: ");
37+
scanf("%d", &rows2);
38+
39+
printf("Enter the number of columns for the second matrix: ");
40+
scanf("%d", &cols2);
41+
42+
// Check if matrices can be multiplied
43+
if (cols1 != rows2) {
44+
printf("Matrices cannot be multiplied. Columns of the first matrix must be equal to rows of the second matrix.\n");
45+
return 1;
46+
}
47+
48+
int matrix1[rows1][cols1], matrix2[rows2][cols2], result[rows1][cols2];
49+
50+
// yaha se 1st matrix ke elemnts ko input karvayenge
51+
printf("Enter elements for the first matrix:\n");
52+
for (int i = 0; i < rows1; i++) {
53+
for (int j = 0; j < cols1; j++) {
54+
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
55+
scanf("%d", &matrix1[i][j]);
56+
}
57+
}
58+
59+
// yaha se 2nd matrix ke elements ko input krvayenge
60+
printf("Enter elements for the second matrix:\n");
61+
for (int i = 0; i < rows2; i++) {
62+
for (int j = 0; j < cols2; j++) {
63+
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
64+
scanf("%d", &matrix2[i][j]);
65+
}
66+
}
67+
68+
?? matrix multiply krvayenge
69+
multiplyMatrices(rows1, cols1, matrix1, rows2, cols2, matrix2, result);
70+
71+
// yaha se matrix display krvayenge
72+
printf("\nFirst Matrix:\n");
73+
displayMatrix(rows1, cols1, matrix1);
74+
75+
printf("\nSecond Matrix:\n");
76+
displayMatrix(rows2, cols2, matrix2);
77+
78+
printf("\nProduct of Matrices:\n");
79+
displayMatrix(rows1, cols2, result);
80+
81+
return 0;
82+
}

0 commit comments

Comments
 (0)