-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperiment3.c
91 lines (76 loc) · 2.43 KB
/
Experiment3.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
#include <stdio.h>
#include <stdlib.h>
int main() {
int A_rows, A_cols, B_rows, B_cols;
// Prompt the user to enter the dimensions of matrix A
printf("Enter the number of rows and columns for matrix A: ");
scanf("%d %d", &A_rows, &A_cols);
// Prompt the user to enter the dimensions of matrix B
printf("Enter the number of rows and columns for matrix B: ");
scanf("%d %d", &B_rows, &B_cols);
// Check if matrix multiplication is possible
if (A_cols != B_rows) {
printf("Matrix multiplication is not possible. A_cols must be equal to B_rows.\n");
return 1;
}
// Dynamic memory allocation for matrix A
int** A = (int**)malloc(A_rows * sizeof(int*));
for (int i = 0; i < A_rows; i++) {
A[i] = (int*)malloc(A_cols * sizeof(int));
}
// Dynamic memory allocation for matrix B
int** B = (int**)malloc(B_rows * sizeof(int*));
for (int i = 0; i < B_rows; i++) {
B[i] = (int*)malloc(B_cols * sizeof(int));
}
// Dynamic memory allocation for the result matrix C
int** C = (int**)malloc(A_rows * sizeof(int*));
for (int i = 0; i < A_rows; i++) {
C[i] = (int*)malloc(B_cols * sizeof(int));
}
// Prompt the user to enter elements for matrix A
printf("Enter the elements of matrix A:\n");
for (int i = 0; i < A_rows; i++) {
for (int j = 0; j < A_cols; j++) {
scanf("%d", &A[i][j]);
}
}
// Prompt the user to enter elements for matrix B
printf("Enter the elements of matrix B:\n");
for (int i = 0; i < B_rows; i++) {
for (int j = 0; j < B_cols; j++) {
scanf("%d", &B[i][j]);
}
}
// Perform matrix multiplication
for (int i = 0; i < A_rows; i++) {
for (int j = 0; j < B_cols; j++) {
C[i][j] = 0;
for (int k = 0; k < A_cols; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
// Display the result matrix C
printf("Matrix A x B:\n");
for (int i = 0; i < A_rows; i++) {
for (int j = 0; j < B_cols; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
// Deallocate memory for matrices A, B, and C
for (int i = 0; i < A_rows; i++) {
free(A[i]);
}
free(A);
for (int i = 0; i < B_rows; i++) {
free(B[i]);
}
free(B);
for (int i = 0; i < A_rows; i++) {
free(C[i]);
}
free(C);
return 0;
}