Trying to find sum of matrices using functions ?

0 votes
asked Nov 11, 2018 by Mohak Kumar (120 points)
#include<stdio.h>

void mysum(int a[][10],int b[][10],int,int);

void mysum(int a[10][10],int b[10][10],int m,int n)

{

  int i,j,sum[10][10];

  for(i=0;i<m;i++)

    {

      for(j=0;j<n;j++)

{

  sum[i][j]=a[i][j]+b[i][j];

}

      printf("\n");

    }

  for(i=0;i<m;i++)

    {

      for(j=0;j<n;j++)

{

  printf("%d",sum[i][j]);

}

      printf("\n");

    }  

}

main ()

{

  int m,n,i,j,a[10][10],b[10][10],sum[10][10];

  printf("enter number of rows needed:");

  scanf("%d",&m);

  printf("enter number of columns needed:");

  scanf("%d",&n);

  printf("\n");

  printf("enter matrix 1:\n");

  for(i=0;i<m;i++)

    {

      for(j=0;j<n;j++)

{

  scanf("%d",&a[i][j]);

}

      printf("\n");

    }

  printf("enter matrix 2:\n");  

  for(i=0;i<m;i++)

  {

      for(j=0;j<n;j++)

{

  scanf("%d",&b[i][j]);

}

      printf("\n");

    }

  mysum(a,m,n,b);

}

1 Answer

0 votes
answered Nov 11, 2018 by murfo

#include <stdio.h>
void mysum(int a[][10],int b[][10],int,int);

void mysum(int a[10][10],int b[10][10],int m,int n) {
  int i,j,sum[10][10];
  for(i=0;i<m;i++) {
      for(j=0;j<n;j++) {
          sum[i][j]=a[i][j]+b[i][j];
      }
      printf("\n");
  }
  for(i=0;i<m;i++) {
      for(j=0;j<n;j++) {
          printf("%d\t",sum[i][j]);  // Separar los términos de la matriz
      }
      printf("\n");
  }  
}

main () {
  int m,n,i,j,a[10][10],b[10][10],sum[10][10];
  printf("enter number of rows needed:");
  scanf("%d",&m);
  printf("enter number of columns needed:");
  scanf("%d",&n);
  printf("\n");
  printf("enter matrix 1:\n");
  for(i=0;i<m;i++) {
      for(j=0;j<n;j++) {
          scanf("%d",&a[i][j]);
      }
      printf("\n");
  }
  printf("enter matrix 2:\n");  
  for(i=0;i<m;i++) {
      for(j=0;j<n;j++) {
          scanf("%d",&b[i][j]);
      }
      printf("\n");
  }
  // mysum(a,m,n,b); Esta forma es incorrecta
  mysum(a,b,m,n);
}

commented Nov 11, 2018 by Mohak Kumar (120 points)
Thank you very much.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...