Skip to main content

Program to add two matrices using pointers.

#include<stdio.h>
main()
{
      int a[5][5],b[5][5],c[5][5],i,j,m,n;
      printf("\n Enter the Dimension of the Matrix:\n");
      scanf("%d%d",&m,&n);
      printf("\nEnter the Elements of the first matix:");
      for(i=0;i<m;i++)
      {
            for(j=0;j<n;j++)
            {
                  scanf("%d",(*(a+i)+j));
            }
      }
      printf("\nEnter the elements of the second matrix:\n");
      for(i=0;i<m;i++)
      {
            for(j=0;j<n;j++)
            {
                  scanf("%d",(*(b+i)+j));
            }
      }
      for(i=0;i<m;i++)
      {
            for(j=0;j<n;j++)
            {
                  *(*(c+i)+j)=*(*(a+i)+j)+ *(*(b+i)+j);
            }
      }
      printf("\nResultant Matrix:\n");
      for(i=0;i<m;i++)
      {
            for(j=0;j<n;j++)
            {
                  printf("%5d",*(*(c+i)+j));
            }
            printf("\n");
      }
      getch();

}

Comments