Tuesday, December 10, 2013

Program in C to find sum and product of two matrices by passing array to function

#include<stdio.h>
void arr(int a[3][3],int b[3][3]);
void main()
{
int a[3][3],b[3][3],c[3][3],d[3][3],i,j;
printf("Enter first matrix\n");
for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
    scanf("%d",&a[i][j]);
    }
   }
printf("You have entered first matrix as\n");
for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
    printf("%d",a[i][j]);
    }
    printf("\n");
   }

printf("Enter second matrix\n");
for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
    scanf("%d \t",&b[i][j]);
    }
  }

printf("You have entered second matrix as\n");
for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
    printf("%d",b[i][j]);
    }
    printf("\n");
   }
arr(a,b);

}


void arr(int a[3][3],int b[3][3])
{
int sum,k,c[3][3],d[3][3],i,j;
 for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    c[i][j]=a[i][j]+b[i][j];
   }
printf("The sum is\n");
for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
    printf("%d",c[i][j]);
    }
    printf("\n");
   }


    for ( i= 0 ; i < 3 ; i++ )
    {
      for ( j = 0 ; j< 3 ; j++ )
      {
        sum=0;
        for ( k = 0 ; k < 3 ; k++ )
        {
          sum = sum + a[i][k]*b[k][j];
        }
        d[i][j] = sum;

      }
    }


  printf("The Product is\n");
for(i=0;i<3;i++)
  {
    for(j=0;j<3;j++)
    {
    printf("%d",d[i][j]);
    }
    printf("\n");
   }
}

2 comments:

  1. Thanks a lot for sharing this amazing knowledge with us. This site is fantastic. I always find great knowledge from it.  Breaking News online

    ReplyDelete
  2. This matrix program clearly shows how to pass arrays to functions in C for sum and product operations. It's clean, easy to understand, and perfect for beginners exploring core logic building. Highly useful when working on matrix operations similar to systems like Mumble 1.3.3 .

    ReplyDelete