-2
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        int row = nums.size();
        int col = nums[0].size();
        vector<vector<int>> newNums;
        if((row*col) < (r*c)){
            return nums;
        }
        else{
            deque<int> storage;
            for(int i = 0; i < row; i++){
                for(int k = 0; k < col; k++){
                    storage.push_back(nums[i][k]);
                }
            }
            for(int j = 0; j < r; j++){
                for(int l = 0; l < c; l++){
                    newNums[j][l] = storage.pop_front();
                }
            }
        }
        return newNums;
    }

Hey guys, I am having a problem where I am getting the said error of the title above 'Void value not ignored as it ought to be'. When I looked up the error message, the tips stated "This is a GCC error message that means the return-value of a function is 'void', but that you are trying to assign it to a non-void variable. You aren't allowed to assign void to integers, or any other type." After reading this, I assumed my deque was not being populated; however, I can not find out why my deque is not being populated. If you guys would like to know the problem I am trying to solve, I will be posting it below. Also, I cannot run this through a debugger since it will not compile :(. Thanks in advance.

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.

You're given a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.

The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.

If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

   Example 1:
    Input: 
    nums = 
    [[1,2],
     [3,4]]
    r = 1, c = 4
    Output: 
    [[1,2,3,4]]
    Explanation:
    The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    Reading some docs never hurts https://en.cppreference.com/w/cpp/container/deque/pop_front – juanchopanza Jun 29 '18 at 22:20
  • `pop_front()` doesn't return the element that was removed from the vector, so you can't assign it to `newNums[j][l]` – Barmar Jun 29 '18 at 22:21

1 Answers1

0

This line has two problems:

newNums[j][l] = storage.pop_front();

First, pop_front() doesn't return the element that was popped. To get the first element of the deque, use storage[0]. Then call pop_front() to remove it.

You also can't assign to newNums[j][i], because you haven't allocated those elements of the vectors. You can pre-allocate all the memory by declaring it like this.

vector<vector<int>> newNums(r, vector<int>(c));

So the above line should be replaced with:

newNums[j][l] = storage[0];
storage.pop_front();
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • What do you mean that I have not allocated those elements of the vector? Are they not empty because I have not set them yet? And ahh, I see pop_front does not return the value, but just removes the element from the list. Would I be able to avoid referring to the position of something with a stack? Because this would cause problems within my for loop because the size of the deque is not the same as the size of the loop constraint and therefore I would not completely search through the deque. – PracticingCodeMan Jun 29 '18 at 22:50
  • Declaring a vector doesn't create any elements in the vector unless you specify the initial size in the declaration. Vector elements cannot be created with subscript assignment. – Barmar Jun 29 '18 at 22:52
  • I haven't actually read the problem statement, so I don't know if your overall algorithm is correct. Your loop doesn't try to search the deque completely, it just puts the first `r*c` elements into `newNums`. – Barmar Jun 29 '18 at 22:55
  • The explanation of `reshape` doesn't say what should happen with the extra elements if `row * col != r * c`. It seems like your code is as correct as can be. – Barmar Jun 29 '18 at 22:58
  • You might also want to read [this](https://stackoverflow.com/questions/1262808/which-stl-container-should-i-use-for-a-fifo). It recommends using `std::queue` rather than `std::deque` for this. – Barmar Jun 29 '18 at 23:00
  • Hey Barmar, I ended up using a stack. Here is a link to my code if you would like to check it out. Thank you for the help. Leetcode Link : https://leetcode.com/problems/reshape-the-matrix/discuss/143430/C++-Solution-Easy-to-Understand-Beats-65 – PracticingCodeMan Jun 29 '18 at 23:15
  • A stack seems wrong for this, it returns the elements in the opposite order that they were inserted. A queue returns them in the same order. – Barmar Jun 29 '18 at 23:17
  • Yeah, afterwards I read that, I see that a queue would be better. Thank you for your help! – PracticingCodeMan Jun 29 '18 at 23:19