Addition with Two Dimensional Arrays

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

Join Date: Feb 2005
Posts: 2
Reputation: dss1984 is an unknown quantity at this point 
Solved Threads: 0
dss1984 dss1984 is offline Offline
Newbie Poster

Addition with Two Dimensional Arrays

 
0
  #1
Feb 9th, 2005
Hi, everyone. This is my first post here. I'm not very good with C++ and I have to write a program using 2 dimensional arrays.

My program is a Menu that works with 2 two dimensional arrays. The program has to generate two random matrices and perform arthimetic procedures like addition and subtraction. My program generates the matrices perfectly, but I'm having a lot of trouble writing the function to add the two matrices. I know that in order to add to matrices you have to sum the elements in every row and column and store them into a new matrix, but I can't figure out how to write the actual code for it. Can anyone help? PLEASE! Here is the code I have written so far for the program. The addition part of the code is being written in the last function.

  1.  
  2. //February 7, 2005
  3.  
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. const int ROW = 3; //global constant for row
  12. const int COL = 3; //global constant for column
  13. const int LOW = 1; //global const for lower limit
  14. const int UP = 10; //global const for upper limit
  15.  
  16. void RandomArray(int[][COL],int, int, int, int); //Random array generator function prototype
  17. void Menu(); //Menu function prototype
  18. void PrintArray(int[][COL],int,int); //Print array function prototype
  19. void AddArray(int[][COL],int[][COL],int,int);
  20.  
  21. int main()
  22. {
  23. srand(time(NULL));
  24. int matrix[ROW][COL]; //Array declaration
  25. int NewMatrix[ROW][COL];
  26. int menuOption;
  27.  
  28.  
  29.  
  30.  
  31. RandomArray(matrix, ROW, COL, LOW, UP); //Function call
  32.  
  33. do
  34. {
  35. Menu();
  36. cin >> menuOption;
  37. cout << endl;
  38.  
  39. switch(menuOption)
  40. {
  41. case 1:
  42. RandomArray(matrix, ROW, COL, LOW, UP);
  43. PrintArray(matrix,ROW,COL);
  44. RandomArray(matrix, ROW, COL, LOW, UP);
  45. PrintArray(matrix,ROW,COL);
  46. AddArray(matrix,NewMatrix,ROW,COL);
  47. PrintArray(matrix,ROW,COL);
  48. break;
  49.  
  50. case 7:
  51. PrintArray(matrix,ROW,COL);
  52. RandomArray(matrix, ROW, COL, LOW, UP);
  53. PrintArray(matrix,ROW,COL);
  54. break;
  55. default:
  56. cout << "Invalid Option." << endl;
  57. break;
  58. }
  59. }while (menuOption != 0);
  60.  
  61.  
  62. return 0;
  63. }
  64.  
  65. void Menu()
  66. {
  67. cout << endl << "Menu Options: " << endl << endl
  68. << "1. Add 2 matrices " << endl
  69. << "2. Subtract 2 matrices " << endl
  70. << "3. Multiply 2 matrices " << endl
  71. << "4. Multiply matrix by a constant " << endl
  72. << "5. Transpose matrix " << endl
  73. << "6. Matrix trace " << endl
  74. << "7. Print " << endl
  75. << "0. EXIT " << endl;
  76. }
  77.  
  78. void RandomArray(int table[ ][COL], int row, int col, int lowerLimit, int upperLimit)
  79. {
  80.  
  81. for (int r = 0; r < ROW; r++)
  82. for (int c = 0; c < COL; c++)
  83. table[r][c] = rand()%(upperLimit - lowerLimit + 1) + lowerLimit;
  84. }
  85.  
  86.  
  87. void PrintArray(int table[][COL], int row, int col)
  88. {
  89. for (row = 0; row < ROW; row++)
  90. {
  91. for (col = 0; col < COL; col++)
  92. cout << setw(3) << table[row][col];
  93. cout << endl;
  94. }
  95. cout << endl;
  96. }
  97.  
  98.  
  99. void AddArrays(int table[][COL], int theMatrix[][COL], int row, int col)
  100. {
  101. for (int r=0; r < row; r++)
  102. for (int c=0; c < col; c++)
  103. theMatrix[r][c]= table[r][c]+ table[r][c];
  104. }

By the way, should my AddArray function be a value-returning function?
Last edited by cscgal; Mar 17th, 2008 at 2:37 pm. Reason: Replaced quote bbcode with code bbcode
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 2
Reputation: dss1984 is an unknown quantity at this point 
Solved Threads: 0
dss1984 dss1984 is offline Offline
Newbie Poster

Re: HELP! Addition with Two Dimensional Arrays

 
0
  #2
Feb 9th, 2005
Nevermind. I just figured it out.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: CloudyBoy is an unknown quantity at this point 
Solved Threads: 0
CloudyBoy CloudyBoy is offline Offline
Newbie Poster

Re: HELP! Addition with Two Dimensional Arrays

 
0
  #3
Mar 17th, 2008
thanks,,, its useful 4 me
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



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC