| | |
Addition with Two Dimensional Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 2
Reputation:
Solved Threads: 0
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.
By the way, should my AddArray function be a value-returning function?
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)
//February 7, 2005 #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int ROW = 3; //global constant for row const int COL = 3; //global constant for column const int LOW = 1; //global const for lower limit const int UP = 10; //global const for upper limit void RandomArray(int[][COL],int, int, int, int); //Random array generator function prototype void Menu(); //Menu function prototype void PrintArray(int[][COL],int,int); //Print array function prototype void AddArray(int[][COL],int[][COL],int,int); int main() { srand(time(NULL)); int matrix[ROW][COL]; //Array declaration int NewMatrix[ROW][COL]; int menuOption; RandomArray(matrix, ROW, COL, LOW, UP); //Function call do { Menu(); cin >> menuOption; cout << endl; switch(menuOption) { case 1: RandomArray(matrix, ROW, COL, LOW, UP); PrintArray(matrix,ROW,COL); RandomArray(matrix, ROW, COL, LOW, UP); PrintArray(matrix,ROW,COL); AddArray(matrix,NewMatrix,ROW,COL); PrintArray(matrix,ROW,COL); break; case 7: PrintArray(matrix,ROW,COL); RandomArray(matrix, ROW, COL, LOW, UP); PrintArray(matrix,ROW,COL); break; default: cout << "Invalid Option." << endl; break; } }while (menuOption != 0); return 0; } void Menu() { cout << endl << "Menu Options: " << endl << endl << "1. Add 2 matrices " << endl << "2. Subtract 2 matrices " << endl << "3. Multiply 2 matrices " << endl << "4. Multiply matrix by a constant " << endl << "5. Transpose matrix " << endl << "6. Matrix trace " << endl << "7. Print " << endl << "0. EXIT " << endl; } void RandomArray(int table[ ][COL], int row, int col, int lowerLimit, int upperLimit) { for (int r = 0; r < ROW; r++) for (int c = 0; c < COL; c++) table[r][c] = rand()%(upperLimit - lowerLimit + 1) + lowerLimit; } void PrintArray(int table[][COL], int row, int col) { for (row = 0; row < ROW; row++) { for (col = 0; col < COL; col++) cout << setw(3) << table[row][col]; cout << endl; } cout << endl; } void AddArrays(int table[][COL], int theMatrix[][COL], int row, int col) { for (int r=0; r < row; r++) for (int c=0; c < col; c++) theMatrix[r][c]= table[r][c]+ table[r][c]; }
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
![]() |
Similar Threads
- compare multi-dimensional arrays and return they keys who's values are different (PHP)
- c++ beginner question one dimensional arrays (C++)
- help passing parrallel arrays to functions (C++)
- Two dimensional Arrays in MIPS (Assembly)
- pointers and multi-dimensional arrays (C++)
- C++ Multi dimensional arrays (C++)
- Multi-dimensional Arrays: (Python)
- three dimensional arrays (PHP)
- How to implement multi-dimensional arrays in Python?? (Python)
Other Threads in the C++ Forum
- Previous Thread: template vs inheritance
- Next Thread: HELp Required....
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





