Tuesday, February 10, 2015

Matrix Multiplication

/* C program to find the result of multiplication of two matrices */ #include <stdio.h> int main() { int a[5][5]; int b[5][5]; int c[5][5]; int i, j, k, sum = 0; int m, n, o, p; printf("Enter the number of rows and columns of first matrix:\n"); scanf("%d %d", &m, &n); printf("\nEnter the row and column of second matrix:\n"); scanf("%d %d", &o, &p); if(n != o) { printf("Matrix multiplication is not possible\n"); printf("Column of first matrix must be same as row of second matrix"); } else { printf("\nEnter the First matrix:\n"); for(i=0; i<m; i++) for(j=0; j<n; j++) scanf("%d", &a[i][j]); printf("\nEnter the Second matrix:\n"); for(i=0; i<o; i++) for(j=0; j<p; j++) scanf("%d", &b[i][j]); printf("\n\nThe First matrix is: "); for(i=0; i<m; i++) { printf("\n"); for(j=0; j<n; j++){ printf("%3d", a[i][j]); } } printf("\n\nThe Second matrix is: "); for(i=0; i<o; i++) { printf("\n"); for(j=0; j<p; j++) { printf("%3d", b[i][j]); } } for(i=0; i<m; i++) for(j=0; j<p; j++) c[i][j] = 0; //row of first matrix for(i=0;i<m;i++) { //column of second matrix for(j=0; j<p; j++) { sum = 0; for(k=0; k<n; k++) sum = sum + a[i][k] * b [k][j]; c[i][j] = sum; } } } printf("\n\nThe multiplication of two matrix is: "); for(i=0; i<m; i++) { printf("\n"); for(j=0; j<p; j++) { printf("%3d", c[i][j]); } } getch(); return 0; }

Share

& Comment

0 comments:

Post a Comment

 

Copyright © 2015 Code Fervor™ is a registered trademark.

Designed by Templateism By Naman Kumar