FAQs in C: Program to Multiply Two Matrices
FAQs in C: Program to Multiply Two Matrices

FAQs in C: Program to Multiply Two Matrices

In this article, we provide a comprehensive guide to multiplying two matrices in C. You’ll find a detailed algorithm, the complete C program, and a step-by-step explanation of each part of the process. This guide is perfect for beginners and intermediate programmers looking to understand matrix multiplication in C.

Algorithm to Multiply Two Matrices

Plaintext
Step 1: Start

Step 2: Input: Read the number of rows and columns for the first matrix (A) and the second matrix (B).

Step 3: Check Compatibility: Ensure that the number of columns in A equals the number of rows in B.

Step 4: Input Elements: Read the elements of matrices A and B.

Step 5: Initialize Result Matrix: Create a result matrix C with dimensions (rows of A x columns of B).

Step 6: Matrix Multiplication:
        For each element C[i][j] in the result matrix:
          (i) Initialize C[i][j] to 0.
          (ii) Compute the sum of the products of corresponding          
               elements in the ith row of A and the jth column of B.

Step 7: Output: Display the result matrix C.
Step 8: End

C Program to Multiply Two Matrices

C
#include <stdio.h>

int main() {
    int aRows, aCols, bRows, bCols;

    // Step 2: Input dimensions of the first matrix
    printf("Enter the number of rows and columns for the first matrix: ");
    scanf("%d %d", &aRows, &aCols);

    // Step 2: Input dimensions of the second matrix
    printf("Enter the number of rows and columns for the second matrix: ");
    scanf("%d %d", &bRows, &bCols);

    // Step 3: Check compatibility for matrix multiplication
    if (aCols != bRows) {
        printf("Matrix multiplication not possible. Number of columns in the first matrix must equal the number of rows in the second matrix.\n");
        return 1;
    }

    // Step 4: Declare matrices
    int A[aRows][aCols], B[bRows][bCols], C[aRows][bCols];

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

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

    // Step 6: Initialize the result matrix and perform multiplication
    for (int i = 0; i < aRows; i++) {
        for (int j = 0; j < bCols; j++) {
            C[i][j] = 0;
            for (int k = 0; k < aCols; k++) {
                C[i][j] += A[i][k] * B[k][j];
            }
        }
    }

    // Step 7: Output the result matrix
    printf("Resultant matrix after multiplication:\n");
    for (int i = 0; i < aRows; i++) {
        for (int j = 0; j < bCols; j++) {
            printf("%d ", C[i][j]);
        }
        printf("\n");
    }

    // Step 8: End
    return 0;
}

Explanation of Each Step

Include Header:

C
#include <stdio.h>

  • This line includes the standard input-output library for using functions like printf and scanf.

Input Dimensions:

C
int aRows, aCols, bRows, bCols;

printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &aRows, &aCols);

printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &bRows, &bCols);

  • These lines read the number of rows and columns for both matrices from the user.

Check Compatibility:

C
if (aCols != bRows) {
    printf("Matrix multiplication not possible. Number of columns in the first matrix must equal the number of rows in the second matrix.\n");
    return 1;
}

  • This condition checks if the matrices can be multiplied. If not, it prints an error message and exits the program.

Declare Matrices and Input Elements:

C
int A[aRows][aCols], B[bRows][bCols], C[aRows][bCols];

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

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

  • These lines declare the matrices and read their elements from the user.

Matrix Multiplication:

C
for (int i = 0; i < aRows; i++) {
    for (int j = 0; j < bCols; j++) {
        C[i][j] = 0;
        for (int k = 0; k < aCols; k++) {
            C[i][j] += A[i][k] * B[k][j];
        }
    }
}

These nested loops perform the matrix multiplication. For each element in the result matrix C, the innermost loop calculates the sum of the products of corresponding elements from A and B.

Output of the Result Matrix:

C
printf("Resultant matrix after multiplication:\n");
for (int i = 0; i < aRows; i++) {
    for (int j = 0; j < bCols; j++) {
        printf("%d ", C[i][j]);
    }
    printf("\n");
}

  • These lines print the result matrix C after multiplication.

End:

C
return 0;

  • This line indicates the successful completion of the program.

This program efficiently multiplies two matrices in C, checking for compatibility and then performing the multiplication, displaying the result matrix at the end.


Discover more from lounge coder

Subscribe to get the latest posts sent to your email.

Leave a Reply

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

Discover more from lounge coder

Subscribe now to keep reading and get access to the full archive.

Continue reading