here the example problem, but I'm still confuse and don't have any idea of solving it... I think this problem is easy to those professional programmers, so please help me to this..
Assume three matrices of the same size, say matrix A, B and C. Write a function that will add the two matrices A and B and store the sum to C. Matrix addition is done component-wise, i.e., C[i ,j] = A[i, j] + B[i, j] where i, j are the row and column index respectively. Pass the three arrays as parameters.
thank you!!
Please help me, any C programmers...?
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
void main()
{
int mat[3][5][5],i,j,k,m,n;
char a[3][4]={"A","B","A+B"};
clrscr();
printf("\n Enter the order of the matrices(m*n)\n\n m= ");
scanf("%d",%26amp;m);
printf(" n= ");
scanf("%d",%26amp;n);
for(i=0;i%26lt;2;i++)
{
clrscr();
printf("\n\n Enter the elements of the matrix %s,\n",a[i]);
for(j=0;j%26lt;m;j++)
{
printf("\n");
for(k=0;k%26lt;n;k++)
{
printf(" %d,%d: ",j+1,k+1);
scanf("%d",%26amp;mat[i][j][k]);
}
}
}
clrscr();
for(i=0;i%26lt;3;i++)
{
printf("\n%s\t=",a[i]);
for(j=0;j%26lt;m;j++)
{
for(k=0;k%26lt;n;k++)
{
printf("\t%d",mat[i][j][k]);
mat[2][i][k]=mat[0][i][j]
+mat[1][i][j];
}
printf("\n\t");
}
}
_setcursortype(_NOCURSOR);
getch();
}
Reply:void MatAdd(int A[], int B[], int C[] in ROWS, int COLS){
/* Function MatAdd receives Matrixes A, B, and C plus the number of Rows and columns in each. Matrixes are arrays which are passed as pointers, so A and B are added with the result stored in C. Returns nothing */
int i, j;
for (j=0;j%26lt;ROWS;j+=1)
for (i=0;i%26lt;COLS;i+=1)
C[i,j]=A[i,j]+B[i,j];
}
Reply:This is a pretty easy problem, I'm not going to explain it
void add( int ** a , int ** b , int ** c, int iSize , int jSize ){
int i , j;
for( i = 0 ; i %26lt; iSize ; i ++ ) {
for( j = 0 ; j %26lt; jSize ; j++ ) {
c[i][j] = a[i][j] + b[i][j];
}
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment