How to pass a matrix as parameter in C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to pass a matrix as parameter in C++

I want to create a function where performs a sum between two matrix, but I don't know how to do it... Void matrixSum(double matrix1[][10], double matrix2[][10]) { ... } Is this the correct way to do it? The problem is that I don't have a fixed array. The size of the array depends of what number the user enter. Double matrix1[size][size]; Double matrix2[size][size]; Where size is the user's number. When I call the function: matrixSum(matrix1, matrix2); Gives me an error: Cannot initialize a parameter of type 'double (*)[10]' with an lvalue of type 'double [this->size][this->size] I don't know how to solve the error and make the function works... Maybe I need dynamic array? How can I make this a dynamic array? (if is the solution)

23rd Jan 2019, 10:21 PM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
8 Answers
+ 9
Well, double matrix1[size][size]; where size is not a compile time constant is not valid C/C++ anyway, so I'm not gonna talk about that here. 2D arrays are quite messy. You would have to use T** where T is the type and allocate correctly, which can be confusing and would look like this: int width, height; std::cin >> width >> height; double** arr = new double*[height]; for( int i = 0; i < height; ++i ) arr[i] = new double[width]; // I know, right and the corresponding deletion would look like: for( int i = 0; i < height; ++i ) // So many deletes :( delete[] arr[i]; delete[] arr; A function accepting this 2D array would be declared like this: template<typename T> void m( T** arr, int width, int height ); // Eww, 2 *'s Lets not even begin about resizing it. ( if need be ) A better option would be to use a vector like this: std::vector<std::vector<double>> arr( height, std::vector<double>(width) ); And the corresponding function: template<typename T> void m( std::vector<std::vector<T>>& arr ); Little bit less code but still, that type is biiiig, you could get it shorter by using a typedef though. An even better option would be to drop the 2D array all together and instead use 1D array and use math to simulate a 2D array. This has the added benefit that it can actually be used together with functions in the std library and it is faster. This is how it would look like: int width, height; std::cin >> width >> height; double* arr = new double[ width * height ]; // Wow, that is short :) delete[] arr; And the functions: // To transform the 2D coordinates into 1D. int to1D( int x, int y, int width ) { return x + y * width; } // To transform 1D coordinate into 2D std::pair<int, int> to2D( int index, int width ) { return std::make_pair( index % width, index / width ); } template<typename T> void m( T* arr, int width, int height ); Of course you could, and I recommend, use a vector here as well. :)
23rd Jan 2019, 11:20 PM
Dennis
Dennis - avatar
+ 6
Dennis is right and I like to do it the way he suggested at the end of his reply; Instead of messing with all of those pointers to pointers, I would actually make a vector of vectors. vector< vector<int> > matrix. PUT SPACES IN BETWEEN YOUR < & > This way, the size can be dynamic AND you don't have to remember to delete (or HOW to delete) all of those dynamically allocated pointers. If you have more questions about this, please ask, Eduardo Perez Regin :)
24th Jan 2019, 12:30 AM
Zeke Williams
Zeke Williams - avatar
+ 5
Very clear explanation Dennis , actually at the begining I used std::vector and I didn't have any problem, but because this is a homework, my teacher said "you won't use <vector> with me..." It was pretty frustrated because now I need to face this problem, managing dynamic Memory, deleting... Ugg... Well your answer makes me realized that to solve this kind of problems I need to manage dynamic memory (if I don't consider std::vectors). I will try a shot solving it using dynamic arrays. Thank you so much!
24th Jan 2019, 12:50 AM
Eduardo Perez Regin
Eduardo Perez Regin - avatar
+ 5
Hatsy Rei not having to delete dynamically allocated pointers has nothing to do with the statement I made about the spaces in between < & >. I have a lot of web developer in me, so I was just ensuring that someone without C++11 would be able to nest vectors and not have an issue; Not everyone uses the most up-to-date IDE and/or compiler.
24th Jan 2019, 2:07 PM
Zeke Williams
Zeke Williams - avatar
+ 3
Eduardo Perez Regin ahaaaa, well I think it's a great lesson in pointers. It will make you realize how helpful vectors and other STL libraries are after you're done though 😄
24th Jan 2019, 1:06 AM
Zeke Williams
Zeke Williams - avatar
+ 3
Zeke Williams Regarding the part about not having to remember to delete dynamically allocated pointers - Does this pertain to your directive on the previous line, of putting spaces in between < and > ? I thought it was just good practice, or to work around the possible problems once posed by template angle brackets. It would be new to me if there are actually more reasons to put spaces between angle brackets when nesting them. https://stackoverflow.com/questions/15785496/c-templates-angle-brackets-pitfall-what-is-the-c11-fix
24th Jan 2019, 1:58 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Ah, that makes sense. I thought that was the case and just want to make sure I wasn't missing out on some awesome tricks!
24th Jan 2019, 2:09 PM
Hatsy Rei
Hatsy Rei - avatar
0
Sup
25th Jan 2019, 1:41 AM
Bentley