POP Lab Manual

Program 6

Implement Matrix Multiplication algorithm with proper validation of multiplication rules. Matrix multiplication is only possible when the number of columns in the first matrix equals the number of rows in the second matrix. The resulting matrix will have dimensions equal to the rows of the first matrix and columns of the second matrix.
⚠️ Multiplication Rule: Matrix A(m×n) × Matrix B(p×q) is valid only if n = p
Matrix Multiplication Formula:
C[i][j] = Σ(A[i][k] × B[k][j]) for k = 0 to n-1
Result Matrix Size: (rows of A) × (columns of B)
Time Complexity: O(m × n × p)

Algorithm

  1. Initialize: Start the program and declare matrix arrays and variables
  2. Input Dimensions: Read rows and columns for both matrices (r1, c1, r2, c2)
  3. Validate Rule: Check if c1 == r2, if not, display error and exit
  4. Input Matrix A: Read elements of first matrix (r1 × c1)
  5. Input Matrix B: Read elements of second matrix (r2 × c2)
  6. Initialize Result: Set all elements of result matrix C to 0
  7. Triple Loop: For each i, j, k: C[i][j] += A[i][k] × B[k][j]
  8. Display Result: Print the resultant matrix C (r1 × c2)
  9. Terminate: End the program

Flowchart

START
Input Dimensions
Validate c1==r2
Input Matrices
Initialize C[i][j]=0
Triple Loop Multiply
Display Result
STOP

Code


#include <stdio.h>


int main() {
    int a[10][10], b[10][10], c[10][10];
    int r1, c1, r2, c2, i, j, k;

    // Input matrix sizes
    printf("Enter rows and columns of first matrix: ");
    scanf("%d %d", &r1, &c1);

    printf("Enter rows and columns of second matrix: ");
    scanf("%d %d", &r2, &c2);

    // Validate multiplication rule
    if (c1 != r2) {
        printf("Matrix multiplication not possible!\n");
        printf("Rule: Columns of first matrix must equal rows of second matrix.\n");
        return 0;
    }

    // Input matrices
    printf("Enter elements of first matrix:\n");
    for (i = 0; i < r1; i++)
        for (j = 0; j < c1; j++)
            scanf("%d", &a[i][j]);

    printf("Enter elements of second matrix:\n");
    for (i = 0; i < r2; i++)
        for (j = 0; j < c2; j++)
            scanf("%d", &b[i][j]);

    // Initialize result matrix to 0
    for (i = 0; i < r1; i++)
        for (j = 0; j < c2; j++)
            c[i][j] = 0;

    // Matrix multiplication
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            for (k = 0; k < c1; k++) {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }

    // Display result
    printf("Resultant Matrix:\n");
    for (i = 0; i < r1; i++) {
        for (j = 0; j < c2; j++) {
            printf("%d ", c[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Sample Output


// Output 1 - Valid Multiplication (2×3 × 3×2)
Enter rows and columns of first matrix: 2 3
Enter rows and columns of second matrix: 3 2
Enter elements of first matrix:
1 2 3
4 5 6
Enter elements of second matrix:
7 8
9 10
11 12
Resultant Matrix:
58 64
139 154

// Output 2 - Invalid Multiplication (2×3 × 2×3)
Enter rows and columns of first matrix: 2 3
Enter rows and columns of second matrix: 2 3
Matrix multiplication not possible!
Rule: Columns of first matrix must equal rows of second matrix.

// Output 3 - Square Matrix Multiplication (2×2 × 2×2)
Enter rows and columns of first matrix: 2 2
Enter rows and columns of 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

// Output 4 - Identity Matrix Multiplication (3×3 × 3×3)
Enter rows and columns of first matrix: 3 3
Enter rows and columns of second matrix: 3 3
Enter elements of first matrix:
1 0 0
0 1 0
0 0 1
Enter elements of second matrix:
5 10 15
20 25 30
35 40 45
Resultant Matrix:
5 10 15
20 25 30
35 40 45

// Output 5 - Single Element Matrices (1×1 × 1×1)
Enter rows and columns of first matrix: 1 1
Enter rows and columns of second matrix: 1 1
Enter elements of first matrix:
7
Enter elements of second matrix:
9
Resultant Matrix:
63
          
Comments

Replay !

0 Comments

Share Your Thoughts

Please enter your name
Please enter a valid email
Password must be at least 6 characters
Please enter your comment
Email Verification Required
We've sent a 6-digit verification code to . Please enter the code below to verify your email address.
Email Verified Successfully!
Your email has been verified. Would you like to proceed with posting your comment?

Type "YES" to confirm and post your comment, or click Cancel to skip posting.