Matrix multiplication in C
×


Matrix multiplication in C

247

Matrix multiplication in C is a process of multiplying two matrices to produce a third matrix.

 Each entry in the resulting matrix is determined by multiplying and summing the elements of the respective row from the first matrix with the corresponding column from the second

Example:

Consider two matrices:

 // Program Matrix multiplication in C
#include<stdio.h> 

int main() {
    int A[2][2] = { {2, 3}, {4, 1} };
    int B[2][2] = { {1, 2}, {3, 4} };
    int C[2][2];
    int i, j, k;

    // Matrix multiplication
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            C[i][j] = 0;
            for(m = 0; m < 2; m++) {
                C[i][j] += A[i][m] * B[m][j];
            }
        }
    }

    // Displaying the result
    printf("Resultant Matrix:\n");
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            printf("%d ", C[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Resultant Matrix:
11 16 
7 12

 We initialize matrices A and B with the given values.

 We then compute the matrix multiplication using nested loops.

 The result is stored in matrix C.

 Finally, we display the resulting matrix C using printf.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments