Tuesday, 23 January 2018

2-D array sum using function

Q: Write a ‘C’ function to calculate matrix C such that C=A+B Use suitable loops. Make use of pointers if necessary

Solution : 

/*==============================
     Girfa Student Help
     Program : 2-D array sum using function
     More Program :http://girfahelp.blogspot.in/p/2-d-array-programming.html
================================*/

#include<stdio.h>
#include<conio.h>
#define MAX 3
void add(int [][MAX],int [][MAX],int [][MAX]);
void print(int [][MAX]);
void main()
{
     int a1[MAX][MAX],a2[MAX][MAX],a3[MAX][MAX],r,c;
     clrscr();
     printf("\nFirst Array Input\n");
     for(r=0;r<MAX;r++)

     {
          for(c=0;c<MAX;c++)
          {
              printf("Enter number>> ");
               scanf("%d",&a1[r][c]);

          }
     }
     printf("\nSecond Array Input\n");
     for(r=0;r<MAX;r++)
     {
          for(c=0;c<MAX;c++)
          {
              printf("Enter number>> ");
              scanf("%d",&a2[r][c]);
          }
     }
     add(a1,a2,a3);
     clrscr();
     printf("\nFirst Array\n");
     print(a1);
     printf("\nSecond Array\n");
     print(a2);
     printf("\nThird Array\n");
     print(a3);
     getch();
}
void add(int a[][MAX],int b[][MAX],int c[][MAX])
{
     int R,C;
     for(R=0;R<MAX;R++)
     {
          for(C=0;C<MAX;C++)
          {
              c[R][C]=a[R][C]+b[R][C];
          }
     }
}
void print(int a[][MAX])
{
     int r,c;
     for(r=0;r<MAX;r++)
     {
          for(c=0;c<MAX;c++)
          {
              printf("\t%d",a[r][c]);
          }
          printf("\n");
     }
}