C Program to Multiply Two Matrices

1. Introduction

Matrix multiplication is a binary operation that produces a matrix from two matrices. For matrix multiplication to be defined, the number of columns in the first matrix should be equal to the number of rows in the second matrix. In this guide, we'll walk you through a C program that multiplies two matrices, followed by a step-by-step explanation of the code.

2. Program Overview

The C program will:

1. Ask the user for the dimensions of the two matrices.

2. Ensure matrix multiplication is possible based on the given dimensions.

3. Take the elements of both matrices as input from the user.

4. Multiply the matrices.

5. Display the resulting matrix.

3. Code Program

#include <stdio.h>

int main() {
    int a[10][10], b[10][10], result[10][10];
    int aRows, aCols, bRows, bCols;
    int i, j, k, sum;

    // Asking user to input the dimensions of the matrices
    printf("Enter number of rows and columns for first matrix: ");
    scanf("%d%d", &aRows, &aCols);
    printf("Enter number of rows and columns for second matrix: ");
    scanf("%d%d", &bRows, &bCols);

    if (aCols != bRows) {
        printf("Matrix multiplication not possible!");
        return 0;
    }

    // Reading elements of first matrix
    printf("Enter elements of first matrix:\n");
    for(i = 0; i < aRows; i++) {
        for(j = 0; j < aCols; j++) {
            scanf("%d", &a[i][j]);
        }
    }

    // Reading elements of second matrix
    printf("Enter elements of second matrix:\n");
    for(i = 0; i < bRows; i++) {
        for(j = 0; j < bCols; j++) {
            scanf("%d", &b[i][j]);
        }
    }

    // Multiplying the matrices
    for(i = 0; i < aRows; i++) {
        for(j = 0; j < bCols; j++) {
            sum = 0;
            for(k = 0; k < aCols; k++) {
                sum += a[i][k] * b[k][j];
            }
            result[i][j] = sum;
        }
    }

    // Displaying the result
    printf("Resultant matrix:\n");
    for(i = 0; i < aRows; i++) {
        for(j = 0; j < bCols; j++) {
            printf("%d\t", result[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Enter number of rows and columns for first matrix: 2 2
Enter number of rows and columns for second matrix: 2 2
Enter elements of first matrix:
1 2
3 4
Enter elements of second matrix:
5 6
7 8
Resultant matrix:
19	22
43	50

4. Step By Step Explanation

1. Variable Declaration:

- Three matrices a, b, and result are declared.

- aRows, aCols, bRows, and bCols store the dimensions of matrices a and b, respectively.

2. Getting Dimensions and Input:

- The program prompts the user to provide the dimensions for both matrices.

- A check is performed to ensure the number of columns in the first matrix equals the number of rows in the second matrix.

- The user is then asked to input the elements of both matrices.

3. Matrix Multiplication:

- Three nested loops are used to perform the matrix multiplication.

- The outer two loops iterate through the rows of the first matrix and the columns of the second matrix.

- The innermost loop, indexed by k, performs the actual multiplication and accumulation for each element of the result matrix.

4. Displaying the Result:

- The program then outputs the multiplied matrix. 

Matrix multiplication is a fundamental operation in linear algebra and finds numerous applications in various mathematical, scientific, and engineering domains.

Comments