Monday, 26 December 2016

2-D Array Matrix Multiplication

2-D Array Matrix Multiplication Rule

  • Matrix a row must be same as Matrix B column
  • Each Cell of Matrix A multiply by each column of matrix B
2-D Array Matrix Multiplication Rule
Code : 


/*   #################################################
          Girfa : Student Help
          Multiplication of 2-d array
          for more program visit : http://girfahelp.blogspot.in/p/c-language-array-programming.html
     #################################################

     */
#include<stdio.h>
#include<conio.h>
void main()
{
     int ar1[3][3],ar2[3][3],ar3[3][3],r,c,m,tmp;
     clrscr();
     printf("first array input\n");
     for(r=0;r<3;r++)
     {
          for(c=0;c<3;c++)
          {
              printf("Enter no ");
              scanf("%d",& ar1[r][c]);
          }
     }
     printf("second array\n");
     for(r=0;r<3;r++)
     {
          for(c=0;c<3;c++)
          {
               printf("Enter no ");
               scanf("%d", &ar2[r][c]);
          }
       }
     /*  Multiplication */
       for(r=0;r<3;r++)
       {
          for(c=0;c<3;c++)
          {
              for(m=0,tmp=0;m<3;m++)
                   tmp+=ar1[r][m]*ar2[m][c];
              ar3[r][c]=tmp;
          }
       }
       clrscr();
       printf("\nFirst array\n");
       for(r=0;r<3;r++)
       {
          for(c=0;c<3;c++)
          {
              printf("\t%d",ar1[r][c]);

          }
          printf("\n");
       }
       printf("\nSecond array\n");
       for(r=0;r<3;r++)
       {
          for(c=0;c<3;c++)
          {
              printf("\t%d",ar2[r][c]);

          }
          printf("\n");
       }
       printf("\nMultiplication of array\n");
            for(r=0;r<3;r++)
            {
                                                                             for(c=0;c<3;c++)
              {
                   printf("\t%d",ar3[r][c]);
              }
              printf("\n");
            }

     getch();

}

No comments:

Post a Comment