0
int main(){
    int row1, column1;
    cout << "How many rows for first matrix?";
    cin >> row1;
    cout << "How many columns first matrix?";
    cin >> column1;
    int org[row1][column1];
    cout << "Enter the elements of first matrix: ";
    for(int i=0;i<row1;i++){
        for(int j=0;j<column1;j++){
            cin>>org[i][j];
        }
    }
    for(int i=0;i<row1;i++){
        for(int j=0;j<column1;j++){
            cout<<org[i][j];
        }
        cout<<endl;
    }
    matrixreshape sol;

I have problems passing column1, row1, and matrix org into the matrixReshape function.

cout<<sol.matrixReshape(vector<vector<int> org[row1][column1],row1, column1);

matrixReshape function as follows:

class matrixreshape {
public:
    vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        vector<vector<int>> newMatrix(r, vector<int>(c, 0));
Azeem
  • 11,148
  • 4
  • 27
  • 40
Justin Ding
  • 415
  • 1
  • 5
  • 16

1 Answers1

1

The method matrixReshape needs a vector<vector<int>> as an actual argument. You are trying to pass it a 2D VLA which is not directly convertible.

You need to use vector<vector<int>> for input and pass that to the matrixReshape.

Here's an example:

vector<vector<int>> org( row1 );

for ( int i = 0; i < org.size(); i++ )
{
    org[i].resize( column1 );
    for ( int j = 0; j < org[i].size(); j++ )
    {
        cin >> org[i][j];
    }
}

// and then pass it
matrixreshape sol;
cout << sol.matrixReshape( org, row1, col1 );

Or, with C++11's range-for loop, it would be like this:

std::size_t rows {0};
std::size_t cols {0};
std::cin >> rows >> cols;

std::vector<std::vector<int>> matrix( rows );
for ( auto& r : matrix )
{
    r.resize( cols );
    for ( auto& c : r )
    {
        std::cin >> c;
    }
}

matrixreshape sol;
std::cout << sol.matrixReshape( matrix, rows, cols );

Here's a complete working example: https://godbolt.org/z/XPwRMq

In addition, you don't need rows and columns information to pass to matrixReshape() method as the std::vector has size() method. You can use that if you need that.

Another thing is that you have to overload stream insertion operator << for this type to print it with std::cout.

Here's an example (live):

#include <iostream>
#include <vector>

using Matrix = std::vector<std::vector<int>>;

std::ostream& operator<<( std::ostream& os, const Matrix& m )
{
    for ( const auto& r : m )
    {
        for ( const auto& c : r )
        {
            os << c << ' ';
        }
        os << '\n';
    }

    return os;
}

int main()
{
    const Matrix m
    {
        { 1, 2 },
        { 3, 4 }
    };

    std::cout << m;

    return 0;
}

Output:

1 2
3 4
Azeem
  • 11,148
  • 4
  • 27
  • 40
  • Thx for the feedback, but I do have another follow up question. When I try to output the new matrix from the main function, I received a new error as following, error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘std::vector >’). how to fix this? – Justin Ding Apr 10 '20 at 14:35
  • https://www.onlinegdb.com/edit/H1PBYknvI. It seems I can't use std::cout to output the matrix. – Justin Ding Apr 10 '20 at 14:44
  • @JustinDing: Just looked at your question. You're trying to print the returning `std::vector` with `std::cout`.You have to overload stream insertion operator `<<` for it. Or, you can simply loop over it and print the values. And, `rows` and `cols` values won't be requireed for `matrixReshape` arguments as you an get the sizes from the input matrix. – Azeem Apr 10 '20 at 14:46
  • @JustinDing: Here's an example: https://godbolt.org/z/p3Phws. – Azeem Apr 10 '20 at 14:51
  • Thx, figured it out. – Justin Ding Apr 10 '20 at 16:07