0

Below did not work. Is there anyother way?

#include<stdio.h>
#include<math.h>

const int No=2;

int* mat_sum(int[][No], int[][No],int);

void main(){


int A[No][No]={{0,2},{0,1}};
int B[No][No]={{0,1},{1,0}};
int *Result=0;

Result=mat_sum(A,B,No);

}


int* mat_sum (int A[][No], int B[][No],int length){
int *C=new int[No][No];


for (int i=0;i<length;i++){
for (int j=0;j<length;j++){
C[j]=0;
C[j]=A[j]+B[j];
// printf("%2d",C[j]);
}

}
return C;

}

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Dave Sinkula
0

You could pass the result matrix as a parameter to your function.

const int No = 2;

void mat_sum (int A[][No], int B[][No], int C[][No], int length)
{
   for ( int i = 0; i < length; i++ )
   {
      for ( int j = 0; j < length; j++ )
      {
         C[i][j] = A[i][j] + B[i][j];
      }
   }
} 

int main()
{
   int A[No][No]={{0,2},{0,1}};
   int B[No][No]={{0,1},{1,0}};
   int C[No][No];
   mat_sum(A,B,C,No);
   return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.