📘 Lesson  ·  Lesson 62

Matrix Multiplication

Matrix Multiplication

Matrix Multiplication

💡 Note

Multiply two matrices using three nested loops — each result cell is a row-times-column sum.

Program

C
#include <stdio.h>
int main() {
    int a[2][2]={{1,2},{3,4}}, b[2][2]={{5,6},{7,8}}, c[2][2]={0};
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<2;k++)
                c[i][j] += a[i][k]*b[k][j];
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++) printf("%d ", c[i][j]);
        printf("\n");
    }
    return 0;
}
Output:
19 22 43 50

Summary

  • Three loops: rows, columns, and the shared dimension k.
  • Each cell c[i][j] sums products a[i][k]*b[k][j].

Matrix Multiplication

💡 Note

तीन nested loops से दो matrices गुणा करें — हर result cell एक row-times-column sum है।

Program

C
#include <stdio.h>
int main() {
    int a[2][2]={{1,2},{3,4}}, b[2][2]={{5,6},{7,8}}, c[2][2]={0};
    for(int i=0;i<2;i++)
        for(int j=0;j<2;j++)
            for(int k=0;k<2;k++)
                c[i][j] += a[i][k]*b[k][j];
    for(int i=0;i<2;i++){
        for(int j=0;j<2;j++) printf("%d ", c[i][j]);
        printf("\n");
    }
    return 0;
}
Output:
19 22 43 50

सारांश

  • तीन loops: rows, columns, और shared dimension k।
  • हर cell c[i][j], a[i][k]*b[k][j] के products जोड़ता है।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n