Skip to content

Commit

Permalink
Merge pull request #248 from Dyfintie/patch-1
Browse files Browse the repository at this point in the history
Create Matrix_multiply.c
  • Loading branch information
AkhzarFarhan authored Oct 22, 2024
2 parents 0ac30d9 + 4034362 commit fd467da
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions C/Matrix_multiply.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <stdio.h>
#include <stdlib.h>

void inputMatrix(int **matrix, int rows, int cols) {
printf("Enter elements of the matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
}

void printMatrix(int **matrix, int rows, int cols) {
printf("The resultant matrix is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int r1, c1, r2, c2;

printf("Enter the number of rows in the 1st matrix: ");
scanf("%d", &r1);
printf("Enter the number of columns in the 1st matrix: ");
scanf("%d", &c1);
printf("Enter the number of rows in the 2nd matrix: ");
scanf("%d", &r2);
printf("Enter the number of columns in the 2nd matrix: ");
scanf("%d", &c2);

// Check if the matrices can be multiplied
if (c1 != r2) {
printf("The matrices cannot be multiplied.\n");
return 1;
}

// Dynamically allocate memory for matrices
int **matrix1 = (int **)malloc(r1 * sizeof(int *));
int **matrix2 = (int **)malloc(r2 * sizeof(int *));
int **result = (int **)malloc(r1 * sizeof(int *));
for (int i = 0; i < r1; i++) {
matrix1[i] = (int *)malloc(c1 * sizeof(int));
result[i] = (int *)malloc(c2 * sizeof(int));
}
for (int i = 0; i < r2; i++) {
matrix2[i] = (int *)malloc(c2 * sizeof(int));
}

// Input elements of the first and second matrices
inputMatrix(matrix1, r1, c1);
inputMatrix(matrix2, r2, c2);

// Performing matrix multiplication
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
result[i][j] = 0; // Initialize result matrix element
for (int k = 0; k < c1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}

// Print the resultant matrix
printMatrix(result, r1, c2);

// Free allocated memory
for (int i = 0; i < r1; i++) {
free(matrix1[i]);
free(result[i]);
}
for (int i = 0; i < r2; i++) {
free(matrix2[i]);
}
free(matrix1);
free(matrix2);
free(result);

return 0;
}

0 comments on commit fd467da

Please sign in to comment.