Multiply Two Matrices by Passing Matrix to a Function in C

Exercise:

Write a C Program to multiply two matrices by passing matrix to a function.

Click Here to View the Solution!
#include <stdio.h>

void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void display(int mult[][10], int rowFirst, int columnSecond);

int main() {
    int firstMatrix[10][10];
    int secondMatrix[10][10];
    int mult[10][10];
    int  rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;
    printf("Enter rows and column for first matrix: ");
    scanf("%d %d", &rowFirst, &columnFirst);
    printf("Enter rows and column for second matrix: ");
    scanf("%d %d", &rowSecond, &columnSecond);
    while (columnFirst != rowSecond) {
        printf("Error! column of first matrix not equal to row of second.\n");
        printf("Enter rows and column for first matrix: ");
        scanf("%d%d", &rowFirst, &columnFirst);
        printf("Enter rows and column for second matrix: ");
        scanf("%d%d", &rowSecond, &columnSecond);
    }
    enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);
    multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond);

    // Function to display resultant matrix after multiplication.
    display(mult, rowFirst, columnSecond);
    return 0;
}

void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond) {
    int i, j;
    printf("\nEnter elements of matrix 1:\n");
    for(i = 0; i < rowFirst; ++i) {
        for(j = 0; j < columnFirst; ++j) {
            printf("Enter elements a%d%d: ", i + 1, j + 1);
            scanf("%d", &firstMatrix[i][j]);
        }
    }
    printf("\nEnter elements of matrix 2:\n");
    for(i = 0; i < rowSecond; ++i) {
        for(j = 0; j < columnSecond; ++j) {
            printf("Enter elements b%d%d: ", i + 1, j + 1);
            scanf("%d", &secondMatrix[i][j]);
        }
    }
}

void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond) {
    int i, j, k;
    for(i = 0; i < rowFirst; ++i) {
        for(j = 0; j < columnSecond; ++j) {
            mult[i][j] = 0;
        }
    }
    for(i = 0; i < rowFirst; ++i) {
        for(j = 0; j < columnSecond; ++j) {
            for(k=0; k<columnFirst; ++k) {
                mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
            }
        }
    }
}

void display(int mult[][10], int rowFirst, int columnSecond) {
    int i, j;
    printf("\nOutput Matrix:\n");
    for(i = 0; i < rowFirst; ++i) {
        for(j = 0; j < columnSecond; ++j) {
            printf("%d  ", mult[i][j]);
            if(j == columnSecond - 1) {
                printf("\n\n");
            }
        }
    }
}
Click Here to View the Output!
Enter rows and column for first matrix: 2 2
Enter rows and column for second matrix: 2 2
Enter elements of matrix 1:
Enter elements a11: 6
Enter elements a12: 5
Enter elements a21: 8
Enter elements a22: 5
Enter elements of matrix 2:
Enter elements b11: 3
Enter elements b12: 9
Enter elements b21: 5
Enter elements b22: 3
Output Matrix:
 43  69  
 49  87  
Click Here to View the Explanation!
  • This program is used to multiply two matrices by passing the matrix to a function.
  • Initially, three void type functions enterData, multiplyMatrices and display are declared.
  • In main(), The program requests the user to enter the rows and columns for the first and second matrices individually and stores them in the variables rowFirst, columnFirst, rowSecond and columnSecond.
  • A check is created using the while loop which checks whether columnFirst is not equal to rowSecond. If true, the program requests the user to enter the size of the matrices again.
  • The functions enterData, multiplyMatrices and display are then called in main for taking the data of the matrices, for multiplying the matrices and for displaying the resultant matrix after the multiplication.
  • Outside main, the functions are initialized in a sequence.
  • The enterData function requests the user to enter the elements of the first and second matrix and stores them using nested for loops inside the 2D arrays firstMatrix[i][j] and secondMatrix[i][j].
  • The second function multiplyMatrices is created that initializes the elements of the final matrix to 0 as mult[i][j] = 0.
  • The function then multiplies the first and second matrices and stores their result in the mult matrix.
  • Finally, the display function is initialized that prints the mult[i][j] matrix column-wise. And once the inner loop reaches to the (coulmnSecond – 1)th value, the loop exits and the outer loop starts executing again for the second row.
%d bloggers like this: