matrix addition

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2008
Posts: 5
Reputation: idb_study is an unknown quantity at this point 
Solved Threads: 0
idb_study idb_study is offline Offline
Newbie Poster

matrix addition

 
0
  #1
Jan 2nd, 2009
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:-
  1. #include<iostream.h>
  2.  
  3. int main()
  4. {
  5. int mat1[3][3];
  6. int mat2[3][3];
  7. int i,j;
  8.  
  9. cout<<"\n1st Matrix: \n"<<flush;
  10. for(i=0;i<3;i++)
  11. {
  12. for(j=0;j<3;j++)
  13. {
  14. cin>>mat1[i][j];
  15. }
  16. }
  17. cout<<"\n2nd Matrix: \n"<<flush;
  18. for(i=0;i<3;i++)
  19. {
  20. for(j=0;j<3;j++)
  21. {
  22. cin>>mat2[i][j];
  23. }
  24. }
  25.  
  26. int mat[3][3];
  27. int k,l;
  28. cout<<"\nResultant: \n"<<flush;
  29. for(k=0;k<3;k++)
  30. {
  31. for(l=0;l<3;l++)
  32. {
  33. mat[k][l]=(mat1[k][l]+mat2[k][l]);
  34. cout<<" "<<mat[l]<<flush;
  35. }
  36. cout<<"\n"<<flush;
  37. }
  38.  
  39. return 0;
  40. }
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
Last edited by Ancient Dragon; Jan 2nd, 2009 at 10:09 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,868
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is online now Online
Posting Virtuoso

Re: matrix addition

 
0
  #2
Jan 2nd, 2009
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
  1. for(k=0;k<3;k++)
  2. {
  3. for(l=0;l<3;l++)
  4. {
  5. mat[k][l]=(mat1[k][l]+mat2[k][l]);
  6. cout<<" "<<(mat[k][l]);
  7. }
  8. cout<<"\n";
  9. }
Also, iostream.h isn't standard. <iostream> is. Which compiler are you using?
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,176
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 147
firstPerson's Avatar
firstPerson firstPerson is online now Online
Veteran Poster

Re: matrix addition

 
0
  #3
Jan 2nd, 2009
your problem is your cout statement.

instead of cout<<mat[l];

it should be cout<<mat[l][k];
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: idb_study is an unknown quantity at this point 
Solved Threads: 0
idb_study idb_study is offline Offline
Newbie Poster

Re: matrix addition

 
0
  #4
Jan 5th, 2009
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
Last edited by idb_study; Jan 5th, 2009 at 1:43 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC