This example will show you how to multiply two matrices using two dimensional array in C program. In this example a user will be asked to enter the number of rows and columns for matrices. Then user will be asked to enter the elements at each index of the matrices.

Here we will see also how to use pointers to allocate memory dynamically for array using malloc function.

The product of two matrices A and B is defined only when the number of columns of A is equal to the number of rows in B.

/* 
 * File:   matrixMult.c
 * Author: https://www.roytuts.com
 *
 */

#include <stdio.h>
#include <stdlib.h>

int ** matrixMult(int **a, int **b, int r, int c) {
    int i, j, k, sum = 0;
    int **result = malloc(r * sizeof (int *));

    for (i = 0; i < r; i++) {
        result[i] = malloc(r * sizeof (int));
    }

    for (i = 0; i < r; i++) {
        for (j = 0; j < r; j++) {
            for (k = 0; k < c; k++) {
                sum += a[i][k] * b[k][j];
            }
            result[i][j] = sum;
            sum = 0;
        }
    }

    return result;
}

int main() {

    int i, j, rows, cols, **a, **b, **result;

    printf("Multiplication of two Matrix");

    printf("\nEnter number of rows :");
    scanf("%d", &rows);
    printf("\nEnter number of columns :");
    scanf("%d", &cols);

    // allocate rows, each row is a pointer to int
    a = malloc(rows * sizeof (int *));
    // allocate cols(rows), each row is a pointer to int
    b = malloc(cols * sizeof (int *));

    // for each row allocate cols int
    for (i = 0; i < rows; i++) {
        a[i] = malloc(cols * sizeof (int));
    }

    for (i = 0; i < cols; i++) {
        b[i] = malloc(rows * sizeof (int));
    }

    printf("\nEnter Matrix A\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            printf("Enter Element %d %d : ", i, j);
            scanf("%d", &a[i][j]);
        }
    }

    printf("\nEnter Matrix B\n");
    for (i = 0; i < cols; i++) {
        for (j = 0; j < rows; j++) {
            printf("Enter Element %d %d : ", i, j);
            scanf("%d", &b[i][j]);
        }
    }

    result = matrixMult(a, b, rows, cols);

    printf("\nMultiplication results of Matrix A & B\n");
    for (i = 0; i < rows; i++) {
        for (j = 0; j < rows; j++) {
            printf("%d ", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output

Multiplication of two Matrix
Enter number of rows :3

Enter number of columns :4

Enter Matrix A
Enter Element 0 0 : 9
Enter Element 0 1 : 1
Enter Element 0 2 : 0
Enter Element 0 3 : -5
Enter Element 1 0 : 6
Enter Element 1 1 : 2
Enter Element 1 2 : 10
Enter Element 1 3 : 0
Enter Element 2 0 : 9
Enter Element 2 1 : -15
Enter Element 2 2 : 5
Enter Element 2 3 : 9

Enter Matrix B
Enter Element 0 0 : 1
Enter Element 0 1 : 2
Enter Element 0 2 : 3
Enter Element 1 0 : 4
Enter Element 1 1 : 5
Enter Element 1 2 : 6
Enter Element 2 0 : 7
Enter Element 2 1 : 8
Enter Element 2 2 : 9
Enter Element 3 0 : 10
Enter Element 3 1 : 11
Enter Element 3 2 : 12

Multiplication results of Matrix A & B
-37 -32 -27 
84 102 120 
74 82 90

Thanks for reading.

Tags:

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on JEE Tutorials Twitter Facebook  Google Plus Linkedin Or Email Me

Leave a Reply

Your email address will not be published. Required fields are marked *