Hi all,
I have tried to write a program which can add two matrix.
It is running well....but when I am printing out the resultant matrix it is just showing the address of the resultant elements.

For convenience, I have posted the output also.

Program:-

#include<iostream.h>

int main()
{
        int mat1[3][3];
        int mat2[3][3];
        int i,j;

        cout<<"\n1st Matrix: \n"<<flush;
        for(i=0;i<3;i++)
        {
                for(j=0;j<3;j++)
                {
                        cin>>mat1[i][j];
                }
        }
        cout<<"\n2nd Matrix: \n"<<flush;
        for(i=0;i<3;i++)
        {
                for(j=0;j<3;j++)
                {
                        cin>>mat2[i][j];
                }
        }

         int mat[3][3];
        int k,l;
        cout<<"\nResultant: \n"<<flush;
        for(k=0;k<3;k++)
        {
                for(l=0;l<3;l++)
                {
                        mat[k][l]=(mat1[k][l]+mat2[k][l]);
                        cout<<" "<<mat[l]<<flush;
                }
                cout<<"\n"<<flush;
        }

        return 0;
}

O/p:

1st Matrix:
1 1 1
1 1 1
1 1 1

2nd Matrix:
1 1 1
1 1 1
1 1 1

Resultant:
0xbfe1b810 0xbfe1b81c 0xbfe1b828
0xbfe1b810 0xbfe1b81c 0xbfe1b828
0xbfe1b810 0xbfe1b81c 0xbfe1b828

Recommended Answers

All 3 Replies

Why are you flushing so much? The problem is that you can't directly print an array. You have to print from both indices, so

for(k=0;k<3;k++)
{
  for(l=0;l<3;l++)
  {
    mat[k][l]=(mat1[k][l]+mat2[k][l]);
    cout<<" "<<(mat[k][l]);
  }
  cout<<"\n";
}

Also, iostream.h isn't standard. <iostream> is. Which compiler are you using?

your problem is your cout statement.

instead of cout<<mat[l];

it should be cout<<mat[l][k];

Thank you very much to all of you.......
sorry for so late reply.....
yes it is working now.......

now the o/p is:-

Resultant:
2 2 2
2 2 2
2 2 2


by the way I'm using gcc compiler

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.