C Program for Matrix Multiplication

Matrix Multiplication

C program to multiply two matrices has been shown here. Matrix $[A]_{m \times n}$ can be multiplied with matrix $[B]_{p \times q}$, iff $n = p$ i.e. the number of columns of the first matrix should to be equal to the number of rows in the second matrix. The order of the resultant matrix $C$ would be $m \times q$.

Algorithm, pseudocode and time complexity of the program have also been shown below.






1. Algorithm for matrix multiplication


1. Take two matrice $A_{m\times n}$ and $B_{p\times q}$ as input.

2. Declare another matrix $C_{m\times q}$ and initailize it with 0.

3. Check if $n = p$

4. If step [3] is false then display "Matrices can not be multiplied!" and go to step [8]

5. If step [3] is true, then

6. Repeat for each $i \in [0, m - 1]$

6.1. Repeat for each $j \in [0, q - 1]$

6.1.1. Repeat for each $k \in [0, p - 1]$

6.1.1.1. Perform $C[i][j] = C[i][j] + A[i][k] * B[k][j]$

7. Display $C_{m\times q}$ as the resultant matrix.

8. Exit program.




2. Pseudocode for matrix multiplication


Input: Two matrices $A_{m\times n}$ and $B_{p\times q}$

Output: $A * B$

1. Procedure matrixMultiplication($A_{m\times n}$, $B_{p\times q}$):

2. Initialize $C_{m\times q} = 0$

3. If $n == p$:

4. Repeat for each $i \in [0, m-1]$

5. Repeat for each $i \in [0, q-1]$

6. Repeat for each $k \in [0, p-1]$

7. $C_{ij}\leftarrow C_{ij} + A_{ik} * B_{kj}$

8. Return $C_{m\times q}$

9. Else:

10. Return Matrices can not be multiplied!

11. End Procedure





3. Time Complexity for matrix multiplication


Time Complexity: O($n^3$)

Where $n$ is the row and column size of two matrices.




4. C Program for matrix multiplication

Code has been copied
/****************************************
        alphabetacoder.com
    C Program for Matrix Multiplication
*****************************************/
#include <stdio.h>

int main() {
    // declare variables
    int m, n, p, q, i, j, k;
    int A[10][10] = {0}, B[10][10] = {0}, C[10][10] = {0};

    //take input of the order of first matrix
    printf("Enter the number of row and column of first matrix: ");
    scanf("%d%d", & m, & n);

    //take input of the first matrix
    printf("Enter the first matrix of order %d x %d =\n", m, n);
    for (i = 0; i < m; i++)
        for (j = 0; j < n; j++)
            scanf("%d", & A[i][j]);

    //take input of the order of second matrix
    printf("Enter the number of row and column of second matrix: ");
    scanf("%d%d", & p, & q);

    //take input of the second matrix
    printf("Enter the second matrix of order %d x %d =\n", p, q);
    for (i = 0; i < p; i++)
        for (j = 0; j < q; j++)
            scanf("%d", & B[i][j]);

    // check if number of columns in first matrix 
    // is same as number of rows in second matrix.
    // If not, then matrices can not be multiplied
    if (n != p)
        printf("\nMatrices can not be multiplied!");
    else {
        // do matrix multiplication
        for (i = 0; i < m; i++) {
            for (j = 0; j < q; j++) {
                for (k = 0; k < p; k++)
                    C[i][j] += A[i][k] * B[k][j];
            }
        }

        printf("The resultant matrix after multiplication:\n");
        //display the result
        for (i = 0; i < m; i++) {
            for (j = 0; j < q; j++) {
                printf("%d ", C[i][j]);
            }
            //new  line
            printf("\n");
        }
    }

    return 0;
}

Output


Case 1:

Enter the number of row and column of first matrix: 3 3

Enter the first matrix of order 3 x 3 =

1 3 4

5 6 1

0 5 2

Enter the number of row and column of second matrix: 3 2

Enter the second matrix of order 3 x 2 =

2 3

1 0

7 1

The resultant matrix after multiplication:

33 7

23 16

19 2



Case 2:

Enter the number of row and column of first matrix: 3 3

Enter the first matrix of order 3 x 3 =

3 5 7

4 5 9

0 1 1

Enter the number of row and column of second matrix: 2 3

Enter the second matrix of order 2 x 3 =

9 4 5

3 7 8


Matrices can not be multiplied!



    This code is a C program that performs matrix multiplication. Here's a step-by-step explanation of the code:
  • The code begins with a comment section that provides some information about the program.
  • The necessary header file, stdio.h, is included, which provides input/output functionality.
  • The main() function is defined, which is the entry point of the program.
  • Several variables are declared:
  • m and n represent the number of rows and columns of the first matrix, respectively.

    p and q represent the number of rows and columns of the second matrix, respectively.

    i, j, and k are loop variables.

  • Three matrices are declared:
  • A represents the first matrix and is initialized with zeros.

    B represents the second matrix and is initialized with zeros.

    C represents the resultant matrix (the product of the two matrices) and is initialized with zeros.

  • The program prompts the user to enter the number of rows and columns of the first matrix.
  • The program then prompts the user to enter the elements of the first matrix using nested loops. The values are stored in the matrix A.
  • The program prompts the user to enter the number of rows and columns of the second matrix.
  • The program then prompts the user to enter the elements of the second matrix using nested loops. The values are stored in the matrix B.
  • The program checks if the number of columns in the first matrix (n) is equal to the number of rows in the second matrix (p). If they are not equal, it displays a message that the matrices cannot be multiplied.
  • If the matrices can be multiplied, the program performs matrix multiplication using nested loops:
  • The outer loop iterates over the rows of the first matrix (A).

    The middle loop iterates over the columns of the second matrix (B).

    The inner loop calculates the dot product of the corresponding row from the first matrix and column from the second matrix, and accumulates the result in the corresponding element of the resultant matrix C.

  • After the multiplication is done, the program displays the resultant matrix by iterating over its elements and printing them.
  • The program ends with a return 0; statement, indicating successful execution.
  • In summary, this program takes two matrices as input from the user, checks if they can be multiplied, performs matrix multiplication if possible, and displays the resultant matrix.