Matrix Multiplication

Matrix Multiplication

Easy C Looping 49 views
Explanation Complexity

Problem Statement

Two 2D arrays (matrices) input. Multiply with nested loops. Logic: result[i][j] = sum of (a[i][k] * b[k][j]). Complex example of triple nested loop.

Input Format

Two 2D arrays (matrices):

• Matrix A of size r1 × c1

• Matrix B of size c1 × c2

Output Format

Resultant matrix of size r1 × c2 after multiplication.


Example

Matrix A (2×3):
1 2 3
4 5 6
Matrix B (3×2):
7  8
9 10
11 12
Result Matrix (2×2):
58  64
139 154

Constraints

• Number of columns of Matrix A = Number of rows of Matrix B

• Matrix sizes ≥ 1

Concept Explanation

Matrix multiplication is performed by multiplying rows of the first matrix with columns of the second matrix.

Step-by-Step Explanation

1.Use an outer loop for rows i of Matrix A.

2.Use a second loop for columns j of Matrix B.

3.Initialize result[i][j] = 0.

4.Use a third (inner) loop for index k from 0 to c1 − 1.

5.In the inner loop:

• Multiply a[i][k] with b[k][j].

• Add the product to result[i][j].

6.After the inner loop ends, result[i][j] contains the final value.

7.Repeat for all rows and columns.

Concept Explanation

Matrix multiplication is performed by multiplying rows of the first matrix with columns of the second matrix.

Step-by-Step Explanation

1.Use an outer loop for rows i of Matrix A.

2.Use a second loop for columns j of Matrix B.

3.Initialize result[i][j] = 0.

4.Use a third (inner) loop for index k from 0 to c1 − 1.

5.In the inner loop:

• Multiply a[i][k] with b[k][j].

• Add the product to result[i][j].

6.After the inner loop ends, result[i][j] contains the final value.

7.Repeat for all rows and columns.

Input / Output Format

Input Format
Two 2D arrays (matrices):

• Matrix A of size r1 × c1

• Matrix B of size c1 × c2
Output Format
Resultant matrix of size r1 × c2 after multiplication.


Constraints
• Number of columns of Matrix A = Number of rows of Matrix B

• Matrix sizes ≥ 1

Examples

Input:
Matrix A (2×3): 1 2 3 4 5 6 Matrix B (3×2): 7 8 9 10 11 12
Output:
Result Matrix (2×2): 58 64 139 154

Example Solution (Public)

C
#include <stdio.h>

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

    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);

    // Matrix multiplication condition
    if (c1 != r2) {
        printf("Matrix multiplication not possible.n");
        return 0;
    }

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

    // Input second matrix
    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++) {
            result[i][j] = 0;
        }
    }

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

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

    return 0;
}

Official Solution Code

#include <stdio.h>

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

    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);

    // Matrix multiplication condition
    if (c1 != r2) {
        printf("Matrix multiplication not possible.n");
        return 0;
    }

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

    // Input second matrix
    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++) {
            result[i][j] = 0;
        }
    }

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

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

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.