Saturday, 18 February 2012

How to multiplies two multidimensional array in C



/*multiplication of two arrays*/


#include<stdio.h>                          // include Header file


void main()
{
      int a[2][2],b[2][2],c[2][2],i,j;//initialize multidimensional array and vriable
     for(i=0;i<=1;i++)                       //loop to store value into the first array
      {
        for(j=0;j<=1;j++)
         {
             printf("enter the value of mat a=");
             scanf("%d",&a[i][j]);
         }
      }
   
for(i=0;i<=1;i++)                  // loop to store value into the second array
      {
         for(j=0;j<=1;j++)
          {
             printf("enter the value of mat b=");
            scanf("%d",&b[i][j]);
          }
      }
     c[0][0]=(a[0][0]*b[0][0])+(a[0][1]*b[1][0]);    //multiplication logic
     c[0][1]=(a[0][0]*b[0][1])+(a[0][1]*b[1][1]);
     c[1][0]=(a[1][0]*b[0][0])+(a[1][1]*b[1][0]);
     c[1][1]=(a[1][0]*b[0][1])+(a[1][1]*b[1][1]);
   printf("\t%d",c[0][0]);
   printf("\t%d",c[0][1]);
   printf("\n\t%d",c[1][0]);
   printf("\t%d",c[1][1]);


}  //End of main

No comments:

Post a Comment

Compiler for C,C++ and Python {paste your programme to see the output}