User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,532 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,893 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2127 | Replies: 2
Reply
Join Date: Sep 2007
Posts: 2
Reputation: fiz is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fiz fiz is offline Offline
Newbie Poster

how to add 2 matrix

  #1  
Oct 5th, 2007
#include<stdio.h>
void AddMatrix(int[2][3],int[2][3],int[2][3],int,int);

int main()
{

int i,j;
int matrixA[2][3], matrixB[2][3], matrixC[2][3];
printf("This Program is to find the summation of matrixA and matrixB\n\n");

for(i=0;i<=1;i++){
for(j=0;j<=2;j++){
printf("Please enter matrixA[%d][%d]:",i+1,j+1);
scanf("%d", &matrixA[i][j]);
}
}

printf("\n\n");
for(i=0;i<=1;i++){
for(j=0;j<=2;j++){
printf("Please enter matrixB[%d][%d]:",i+1,j+1);
scanf("%d", &matrixB[i][j]);
}
}
printf("\n\n");
AddMatirix(matrixA,matrixB,matrixC);
return 0;
}
void AddMatrix(int matrixC[2][3], int matrixA[2][3], int matrixB[2][3], int i, int j)
{

for(i=0;i<=1;i++){
for(j=0;j<=2;j++){

matrixC[2][3] = matrixA[i][j] + matrixB[i][j];

}
}


for(i=0;i<=1;i++){
for(j=0;j<=2;j++){
printf("matrixC is the summation of both matrixA and matrixB: %d", matrixC[i][j]);
}
}
printf("\n\n");
return;
}




please help this out..
I'm confused..
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Location: Athens, Greece
Posts: 199
Reputation: n.aggel is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 9
n.aggel's Avatar
n.aggel n.aggel is offline Offline
Junior Poster

Re: how to add 2 matrix

  #2  
Oct 5th, 2007
please use code tags...so that your code is more readable...this way more people can help you....

for c++ code, type [ code=c++]//your code goes here[/code]

e.g.

  1. #include<stdio.h>
  2. void AddMatrix(int[2][3],int[2][3],int[2][3],int,int);
  3.  
  4. int main()
  5. {
  6.  
  7. int i,j;
  8. int matrixA[2][3], matrixB[2][3], matrixC[2][3];
  9. printf("This Program is to find the summation of matrixA and matrixB\n\n");
  10.  
  11. for(i=0;i<=1;i++){
  12. for(j=0;j<=2;j++){
  13. printf("Please enter matrixA[%d][%d]:",i+1,j+1);
  14. scanf("%d", &matrixA[i][j]);
  15. }
  16. }
  17.  
  18. printf("\n\n");
  19. for(i=0;i<=1;i++){
  20. for(j=0;j<=2;j++){
  21. printf("Please enter matrixB[%d][%d]:",i+1,j+1);
  22. scanf("%d", &matrixB[i][j]);
  23. }
  24. }
  25. printf("\n\n");
  26. AddMatirix(matrixA,matrixB,matrixC);
  27. return 0;
  28. }
  29. void AddMatrix(int matrixC[2][3], int matrixA[2][3], int matrixB[2][3], int i, int j)
  30. {
  31.  
  32. for(i=0;i<=1;i++){
  33. for(j=0;j<=2;j++){
  34.  
  35. matrixC[2][3] = matrixA[i][j] + matrixB[i][j];
  36.  
  37. }
  38. }
  39.  
  40.  
  41. for(i=0;i<=1;i++){
  42. for(j=0;j<=2;j++){
  43. printf("matrixC is the summation of both matrixA and matrixB: %d", matrixC[i][j]);
  44. }
  45. }
  46. printf("\n\n");
  47. return;
  48. }
Last edited by n.aggel : Oct 5th, 2007 at 12:34 pm.
Two roads diverged in a wood, and I— I took the one less traveled by, and that has made all the difference.

by Robert Frost the "The Road Not Taken"
Reply With Quote  
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation: vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough vmanes is a jewel in the rough 
Rep Power: 6
Solved Threads: 97
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Shark

Re: how to add 2 matrix

  #3  
Oct 5th, 2007
First, check your spelling. In line 26, you call function AddMatirix, which of course does not exist.

Your function is written with two int parameters, i and j, which do not convey any information to the function, as written. Why not delete those parameters, declare loop variables in the function.

In line 35, where you store the sum of each pair of elements of the matrix, you are using fixed indexes into matrixC. It should use the loop variables the same as the A and B matrices, as in:
matrixC[i][j] = matrixA[i][j] + matrixB[i][j];

Also, matrixC[2][3] is an element outside the bounds of your matrix. The last legal element is matrixC[1][2]

In the function header, why do you have the matrices out of order.
void AddMatrix(int matrixC[2][3], int matrixA[2][3], int matrixB[2][3], int i, int j)

What you end up doing is storing to the second source matrix the results of adding the first source and the destination, as seen from the main function. Your display of resulting C matrix is in fact showing you the initial content of the B matrix.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 4:33 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC