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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
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<n; i++) {
		for (int j=0; j<m; j++) {
			matrix[i][j]=vec[(m*i)+j];
		} 
	}	
	
	// Matrix display
	
	for (int i=0; i<n; i++) {
		for (int j=0; j<m; j++) {
			cout << matrix[i][j];
		}
		cout << "\n";
	}	
	cin.get();
	
	return 0;
}

// I want to write a function for this code that takes a 
// vector and transforms it into a nXm matrix for further
// use as a variable. How do I define the arguments?

void vecToMatrix(int vec[], int n, int m, int matrix[][])
// This is obviously not going to be the end of the code. Once I can set up the function I will just tweak the first part. 



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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
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<n; i++) {
		for (int j=0; j<m; j++) {
			matrix[i][j]=vec[(m*i)+j];
		}
	}
	
	// Matrix display just to see if it works
	
	for (int i=0; i<n; i++) {
		for (int j=0; j<m; j++) {
			cout << matrix[i][j];
		}
		cout << "\n";
	}	
	
}

int main() {
	
	int vector[]={1,2,3,4,5,6,7,8};
	vecToMatrix(vector,2,4);
	
	cin.get();
	
	return 0;
}

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>
#include <vector>
#include <iterator>

class Matrix
{
public:
    typedef std::vector<int> container_type;

    Matrix( std::size_t rows, std::size_t columns, int value=0 )
        : _rows(rows), _cols(columns), _data(_rows*_cols, value) {}

    template <typename ContainerIter>
    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<m.rows(); ++i )
    {
        for ( unsigned j=0; j<m.cols(); ++j )
            std::cout << std::setw(5) << m(i,j) ;
        std::cout << '\n' ;
    }
    std::cout << std::endl ;
}

int main()
{
    Matrix matrix(5,4) ;
    print(matrix) ;

    for ( unsigned r=0; r<matrix.rows(); ++r )
        for ( unsigned c=0; c<matrix.cols(); ++c )
            matrix(r,c) = (r+1)*(c+1) ;

    print(matrix) ;

    int array[]={1,2,3,4,5,6,7,8};
    std::size_t arrayElements = sizeof(array) / sizeof(array[0]) ;

    Matrix m(array, array+arrayElements, 2, 4) ;
    print(m) ;
}


Topic archived. No new replies allowed.