mrnutter

Matrix Multiply

Jan 29th, 2013
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main() {
  4.         int a[100][100],b[100][100],c[100][100],ax,ay,bx,by,i,j,k;
  5.         printf("A Col: ");
  6.         scanf("%d",&ax);
  7.         printf("A Row: ");
  8.         scanf("%d",&ay);
  9.         printf("B Col: ");
  10.         scanf("%d",&bx);
  11.         printf("B Row: ");
  12.         scanf("%d",&by);
  13.  
  14.         if(ay==bx) {
  15.                 printf("Enter Matrix A:\n");
  16.                 for(i=0; i<ay; i++) for(j=0; j<ax; j++) scanf("%d",&a[i][j]);
  17.                 printf("Enter Matrix B:\n");
  18.                 for(i=0; i<by; i++) for(j=0; j<bx; j++) scanf("%d",&b[i][j]);
  19.                 for(i=0; i<ay; i++) {
  20.                         for(j=0; j<bx; j++) {
  21.                                 c[i][j]=0;
  22.                                 for(k=0; k<ax; k++) {
  23.                                         c[i][j]+=a[i][k]*b[k][j];
  24.                                 }
  25.                         }
  26.                 }
  27.                 for(i=0; i<ay; i++) {
  28.                         for(j=0; j<bx; j++) {
  29.                                 printf("%d\t",c[i][j]);
  30.                         }
  31.                         printf("\n");
  32.                 }
  33.         } else {
  34.                 printf("Can't multiply between matrix A and B\n");
  35.         }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment