### Function Argument Trouble

Hello! My name is Nick and I am in the process of teaching myself C++.

I have a question about a code I am writing to transform a vector/array into a nXm matrix. The first part of my code works fine. However when I go to write it as a function I cannot get past the arguments without an error. Any suggestions?

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include using namespace std; // I want to convert a row vector into an nXm matrix int main() { int vec[]={1,1,1,2,2,2,3,3,3,4,4,4,5,5,5}; // row vector const int n=3; // desired matrix rows const int m=5; // desired matrix columns int matrix[n][m]; // delcaration of matrix // Vector to matrix coversion for (int i=0; i

Thanks,
Nick
In general, if you have an error you should publish it with your question.

The basic solution is to use vectors rather than arrays.

I didn't know this would cause an error, but given the fact that arrays become pointers when passed to functions, it is not surprising. A google search shows that all arrays 'flatten' when passed.

If you are in a really time critical section and are trying to avoid multi-dimensional vectors, you can always pass the array pointer and the dimensions.
What errors are you getting when you try and create the function? Can you post the code for your attempt of creating and using the function, along with the errors?
closed account (3qX21hU5)
The only error that I see in your function prototype is this `int matrix[][]` for multi dimensional arrays as parameters in the a function the first dimension's size doesn't need to be specified BUT any dimension after that does need to be.

so you can do this

`int function(int array[]);`

but you cant do this

`int function(int array[][]);`

because the second dimension needs to have a size something like this `int function(int array[][5]);`. I assume that is the error you are getting.
Last edited on
Since he passes n and m, I would think int ** would be OK.
 Since he passes n and m, I would think int ** would be OK.

Nope. =P

This doesn't make sense really. You need to know the dimensions at compile time to define the matrix, so you might as well just define it directly if this is the way that's chosen to go about it.

A more useful approach might be to make a (matrix) class that acts as a two dimensional array that you could specify sizes for or perhaps just use a vector of vectors.
Thank you guys, I appreciate all of the responses. Sorry for not posting my error.

Let me modify my question slightly. I got my function to work, but I want the function to create a matrix that I can reference outside of the function. How do I do this? Here is my modified code.

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include using namespace std; // I want to convert a row vector into an nXm matrix void vecToMatrix(int vec[], int n, int m) { // Matrix creation int matrix[n][m]; for (int i=0; i
 I got my function to work

Except that what you've posted is not valid C++. In C++, array dimensions must be compile time constants. You may want to disable whatever compiler extension you have enabled that allows that.

The easiest way to do what you want is to create your own type, and use it in place of a two dimensional array, but I suspect this has not yet been introduced to you.

The following is a naive matrix class. I say naive because all error checking is eschewed for simplicity's sake.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````#include #include #include #include class Matrix { public: typedef std::vector container_type; Matrix( std::size_t rows, std::size_t columns, int value=0 ) : _rows(rows), _cols(columns), _data(_rows*_cols, value) {} template Matrix( ContainerIter begin, ContainerIter end, std::size_t rows, std::size_t cols ) : _rows(rows), _cols(cols), _data(begin, end) {} int operator()(std::size_t row, std::size_t col) const { return _data[row * _cols + col] ; } int& operator()(std::size_t row, std::size_t col) { return _data[row * _cols + col] ; } std::size_t rows() const { return _rows ; } std::size_t cols() const { return _cols ; } private: size_t _rows ; size_t _cols ; container_type _data ; }; void print(const Matrix& m) { for ( unsigned i=0; i

Topic archived. No new replies allowed.