Return vector in function

Hello
I have to generate three matrices (arrays) that are 5x5 and have random numbers (0-100) by using two functions. First function has 1 matrix and the second function 2 matrices. I also have to check what array has the highest sum and print out that array. So I am using push.back in first function and want to return it. And then check in the second array if array 2 or 3 has higher sum, then it will replace it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int matrix1(){
std::vector<int> matrix;
***
//generate random 5x5 array
matrix.push_back(random_number);
***
return matrix;}

int matrix2_3{
std::vector<int> matrix;
matrix = matrix1();
//generate random 5x5 array
//check if array 2 or 3 has higher sum
return matrix;}

int main(){
std::vector<int> matrix;
matrix = matrix2_3();
//output array with highest sum
}

My question is, is there a way to return a vector. Or am I making a mistake and there is a easier way.
Thank you
Last edited on
If your function returns a vector, then the function must say so in its return type:

1
2
3
4
5
6
std::vector<int> matrix1()  // see the return type?
{
  std::vector<int> matrix;
  //generate random 5x5 array
  return matrix;
}
by using two functions. First function has 1 matrix and the second function 2 matrices.

That makes little sense. Homework that forces "unnatural" solutions ...


Never mind, you do show
1
2
3
4
5
6
T foo()
{
  U bar;
  // code
  return bar;
}

where T = int and U = std::vector<int>.
If you have written such code and attempted to compile, the compiler should say that it does not know how to convert std::vector<int> into single int.

That is not even necessary. std::vectors are objects that have copy constructor and copy assignment operators; they can be copied, as in return value of a function.
http://www.cplusplus.com/reference/vector/vector/operator=/
http://www.cplusplus.com/reference/vector/vector/vector/

In other words, this is legal:
1
2
3
4
5
6
7
8
9
10
std::vector<int> foo()
{
  std::vector<int> bar;
  // code
  return bar;
}

int main() {
  std::vector<int> m0 = foo();
}



std::vector is essentially a 1D array. One can use index math to treat it as a 2D array.
Alternatively, one can create a vector that stores vectors, e.g. std::vector<std::vector<int>>.


The assignment in general ... if not arbirarily restricted ... I would write:
* a function that returns "matrix" of some sort, filled with numbers
* a function that computes the sum of such matrix
* a function that prints a matrix

The first function is called three times to create three matrices.
The second function is called three times to get three sums.
The last function is used to print the matrix that corresponds to the largest sum.
I have made some corrections and the code is working now, but I am facing with a new problem. How do I print out 5x5 vector. This is rignt now the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
std::vector<int> matrix1(){
std::vector<int> matrix;
***
//generate random 5x5 array
matrix.push_back(random_number);
***
return matrix;}

std::vector<int> matrix2_3{
std::vector<int> matrix;
matrix = matrix1();
//generate random 5x5 array
//check if array 2 or 3 has higher sum
return matrix;}

int main(){
std::vector<int> matrix;
matrix = matrix2_3();
//output array with highest sum
}

I have tried printing 2D vector with this and some other codes, but get error
left of '.size' must have class/struct/union
subscript requires array or pointer type
1
2
3
4
5
6
7
8
for (std::vector<std::vector<int>>::size_type i = 0; i < matrix.size(); i++)
{
	for (std::vector<int>::size_type j = 0; j < matrix[i].size(); j++)
	{
		std::cout << matrix[i][j] << ' ';
	}
	std::cout << std::endl;
}

Is there an easier way to output my vector(matrix) 5x5 without rewriting matrix to vector of vectors?
Check: the "matrix" is a 1D vector<int> that has 25 elements?

1
2
3
4
5
6
7
8
9
10
11
12
13
const size_t ROWS = 5;
const size_t COLS = 5;
if ( COLS*ROWS == matrix.size() )
{
  for ( size_t row = 0; row < ROWS; ++row )
  {
    for ( size_t col = 0; col < COLS; ++col )
    {
      std::cout << matrix[ row*COLS+col ] << ' ';
    }
    std::cout << '\n';
  }
}
Thanks @keskiverto for the quick replay. Also to your first comment on how would you do, I can't do differently, because the task says so.
Topic archived. No new replies allowed.