Thursday, 11 September 2014

Matrix Multiplication program in C Language using arrays With Example


Matrix multiplication program in C is used to perform a multiplication operation. This C program of matrix multiplication is totally depends on an arrays concept. This is based on three dimensional matrix formats. Now the matrix will be multiplied by only the mathematical form. Multiply the matrix in a form of (2×3). Before going to enter this topic, We’ve already published an article about Matrix addition source code in C.  Game coding in C program Matrix multiplication code:

Q: Write a c program for Matrix multiplication using arrays concept.

//Header files
#include<stdio.h>
 main()
 {
  //Program variables
  int d[10][10],c[10][10],b[10][10],m,n,i,j,ip,q,p,k;
  clrscr(); //Function to clear previous output
  printf("input row n column of matrix A: "); //Display fnction
  scanf("%d %d", &m,&n);//Getting input function
  printf("Input row and column of B matrix: ");
  scanf("%d %d",&k,&q);
  if(n==k) //Conditional statement
   {
    printf("matrices can be multiplied\n");
    printf("resultant matrix is %d X %d\n",m,q);
    printf("input A-matrix\n");
    read_mat(d,m,n);
    printf("input B-matrix\n");
    read_mat(b,k,q);
    for(i=0;i<m;++i) //Looping statement(nested for loop)
     for(j=0;j<q;++j)
     {
          c[i][j]=0;
          for(ip=0;ip<n;++ip)
          c[i][j]=c[i][j] + d[i][ip] * b[ip][j];
      }
      printf(" Resultant of A and B matrices :\n");
      write_mat(c,m,q);
      }
      else
       {
    printf(" Col of matrix a must be equal to row of matrix B\n");
    printf("matrices cannot be multiplied :\n");
    }
      }
      read_mat(d,m,n)
       int d[10][10],m,n;
     {
      int i,j;
       for(i=0; i<m; i++)
       for(j=0; j<n; j++)
        scanf("%d",&d[i][j]);
      }
      write_mat(d,m,n)
      int d[10][10],m,n;
       {
         int i,j;
         for(i=0; i<m; i++)
          {
        for(j=0; j<n; j++)
         printf("%5d", d[i][j]);
         printf("\n");
     }
 getch();
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.