Unlocking the Power of Matrix Multiplication

Setting the Stage for Matrix Multiplication

To begin, let’s establish the groundwork for our program. We’ll ask the user to input the size of two matrices, including the number of rows and columns for each. However, there’s a catch – the number of columns in the first matrix must match the number of rows in the second matrix. This fundamental rule ensures that matrix multiplication is possible.


#include <stdio.h>

int getMatrixSize() {
    int size;
    printf("Enter the size of the matrix: ");
    scanf("%d", &size);
    return size;
}

Crafting the Perfect Functions

To tackle this challenge, we’ve created three essential functions:

  • getMatrixElements(): This function takes care of collecting matrix elements from the user.
  • multiplyMatrices(): As its name suggests, this function performs the actual matrix multiplication.
  • display(): Finally, this function showcases the resulting matrix after multiplication has taken place.

void getMatrixElements(int matrix[][10], int row, int col) {
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            printf("Enter element [%d][%d]: ", i + 1, j + 1);
            scanf("%d", &matrix[i][j]);
        }
    }
}

void multiplyMatrices(int matrix1[][10], int matrix2[][10], int result[][10], int row1, int col1, int row2, int col2) {
    for (int i = 0; i < row1; i++) {
        for (int j = 0; j < col2; j++) {
            result[i][j] = 0;
            for (int k = 0; k < col1; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }
}

void display(int matrix[][10], int row, int col) {
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

Putting it all Together

With our functions in place, let’s walk through the process. The user starts by entering the dimensions and elements of both matrices, ensuring that the columns of the first matrix align with the rows of the second. Next, the multiplyMatrices function takes center stage, iterating through the elements of both matrices and storing the results in the result[][] matrix.


int main() {
    int row1, col1, row2, col2;

    printf("Enter the number of rows for matrix 1: ");
    row1 = getMatrixSize();
    printf("Enter the number of columns for matrix 1: ");
    col1 = getMatrixSize();

    printf("Enter the number of rows for matrix 2: ");
    row2 = getMatrixSize();
    printf("Enter the number of columns for matrix 2: ");
    col2 = getMatrixSize();

    if (col1!= row2) {
        printf("Matrix multiplication is not possible.\n");
        return 1;
    }

    int matrix1[10][10], matrix2[10][10], result[10][10];

    printf("Enter elements of matrix 1:\n");
    getMatrixElements(matrix1, row1, col1);

    printf("Enter elements of matrix 2:\n");
    getMatrixElements(matrix2, row2, col2);

    multiplyMatrices(matrix1, matrix2, result, row1, col1, row2, col2);

    printf("Resultant matrix:\n");
    display(result, row1, col2);

    return 0;
}

The Final Product

The end result? A seamless matrix multiplication process that yields accurate results. By harnessing the power of C programming and clever function design, we’ve created a system that simplifies complex mathematical operations. Whether you’re a seasoned developer or just starting out, this example serves as a valuable resource for mastering matrix multiplication.

Leave a Reply