943,998 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 28902
  • C++ RSS
Feb 9th, 2005
0

Addition with Two Dimensional Arrays

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dss1984 is offline Offline
2 posts
since Feb 2005
Feb 9th, 2005
-1

Re: HELP! Addition with Two Dimensional Arrays

Nevermind. I just figured it out.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dss1984 is offline Offline
2 posts
since Feb 2005
Mar 17th, 2008
0

Re: HELP! Addition with Two Dimensional Arrays

thanks,,, its useful 4 me
Reputation Points: 10
Solved Threads: 0
Newbie Poster
CloudyBoy is offline Offline
1 posts
since Mar 2008
Feb 18th, 2011
-1

hiii... this is also the simplest way to calculate the tow arrays..

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],d[10][10],i,j,r,c;
clrscr();
printf("enter the value for ROWS:");
scanf("%d",&r);
printf("enter the value of COLUMNS:");
scanf("%d",c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("enter the element:");
scanf("%",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t %d",a[i][j]);
}
{
printf("\n");
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("enter the element:");
scanf("%",&b[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t %d",b[i][j]);
}
{
printf("\n");
}
}
printf("answer of a+b:\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\t %d",d[i][j]);
}
{
printf("\n");
}
}
getch();
}
this programe is done to get two nos. from the user and print the entered no. into rows and columns and acccordingly add both the matrix.....
Reputation Points: 9
Solved Threads: 0
Newbie Poster
ameya potnis is offline Offline
1 posts
since Feb 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: minor codes in TAO (CORBA)
Next Thread in C++ Forum Timeline: Compare pressed keyboard input and static text MFC C++





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC