0

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

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by idb_study
0

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?

0

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

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.