C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Dynamic Memory Allocation in C

Matrix multiplication using dynamic memory

C Program: Matrix multiplication using dynamic memory

C

#include <stdio.h>

#include <stdlib.h>

 

int main() {

    int **A, **B, **C;

    int r1, c1, r2, c2, i, j, k;

 

    // Step 1: Input dimensions of matrices

    printf("Enter number of rows for Matrix A: ");

    scanf("%d", &r1);

    printf("Enter number of columns for Matrix A: ");

    scanf("%d", &c1);

    printf("Enter number of rows for Matrix B: ");

    scanf("%d", &r2);

    printf("Enter number of columns for Matrix B: ");

    scanf("%d", &c2);

 

    // Step 2: Validate matrix multiplication rule

    if (c1 != r2) {

        printf("\nMatrix multiplication not possible!\n");

        printf("Columns of A must equal rows of B.\n");

        return 1;

    }

 

    // Step 3: Allocate memory dynamically for A, B, and C

    A = (int**) malloc(r1 * sizeof(int*));

    B = (int**) malloc(r2 * sizeof(int*));

    C = (int**) malloc(r1 * sizeof(int*));

 

    if (A == NULL || B == NULL || C == NULL) {

        printf("Memory allocation failed!\n");

        return 1;

    }

 

    for (i = 0; i < r1; i++)

        A[i] = (int*) malloc(c1 * sizeof(int));

    for (i = 0; i < r2; i++)

        B[i] = (int*) malloc(c2 * sizeof(int));

    for (i = 0; i < r1; i++)

        C[i] = (int*) malloc(c2 * sizeof(int));

 

    // Step 4: Input Matrix A

    printf("\nEnter elements of Matrix A:\n");

    for (i = 0; i < r1; i++) {

        for (j = 0; j < c1; j++) {

            printf("A[%d][%d] = ", i, j);

            scanf("%d", &A[i][j]);

        }

    }

 

    // Step 5: Input Matrix B

    printf("\nEnter elements of Matrix B:\n");

    for (i = 0; i < r2; i++) {

        for (j = 0; j < c2; j++) {

            printf("B[%d][%d] = ", i, j);

            scanf("%d", &B[i][j]);

        }

    }

 

    // Step 6: Matrix Multiplication Logic

    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];

            }

        }

    }

 

    // Step 7: Display Resultant Matrix

    printf("\nResultant Matrix (A x B):\n");

    for (i = 0; i < r1; i++) {

        for (j = 0; j < c2; j++) {

            printf("%d\t", C[i][j]);

        }

        printf("\n");

    }

 

    // Step 8: Free dynamically allocated memory

    for (i = 0; i < r1; i++) free(A[i]);

    for (i = 0; i < r2; i++) free(B[i]);

    for (i = 0; i < r1; i++) free(C[i]);

    free(A);

    free(B);

    free(C);

 

    return 0;

}

Output

 
OUTPUT :
Enter number of rows for Matrix A: 2
Enter number of columns for Matrix A: 3
Enter number of rows for Matrix B: 3
Enter number of columns for Matrix B: 2

Enter elements of Matrix A:
A[0][0] = 1
A[0][1] = 2
A[0][2] = 3
A[1][0] = 4
A[1][1] = 5
A[1][2] = 6

Enter elements of Matrix B:
B[0][0] = 7
B[0][1] = 8
B[1][0] = 9
B[1][1] = 10
B[2][0] = 11
B[2][1] = 12

Resultant Matrix (A x B):
58  64
139 154


Explanation

Step

Description

c1 == r2

Condition required for valid matrix multiplication.

C[i][j] += A[i][k] * B[k][j];

Multiplies corresponding row & column elements and sums them up.

malloc()

Dynamically allocates memory for all three matrices.

free()

Frees all dynamically allocated memory to prevent leaks.