c++vectorwhile-loopreturndo-while

While loop does not work in C++ when I have return inside it


I'm new to C++ and working on the following code, which rotates a given 2D matrix. My code works fine for one rotation. Then I add a while loop to my code to make it more general, I noticed that the while code does not go to more iteration no matter what. I tried to bring the return MatrixRotateOutPut out of the while but then it was unknown.

I also tried to add return 0 after while loop but it gives me another error. I also checked these two links (Link 1, Link 2), but they were not really helpful for me.

#include<iostream>
#include<vector>

using namespace std;

int MatrixPrint2D(vector<vector<int>> Input) {
    for (int i = 0; i < Input.size(); i++) {
        for (int j = 0; j < Input[0].size(); j++) {
            printf("%d ", Input[i][j]);
        }
        printf("\n");
    }
}


vector<vector<int>> MatrixRotate(vector<vector<int>> Input, int Irritation) {

    while (Irritation > 0) {
        cout << Irritation << "\n" << endl;

        vector<vector<int>> MatrixRotateOutPut(Input[0].size(), vector<int>(Input.size(), 1));

        for (int i = 0; i < Input.size(); i++) {
            for (int j = 0; j < Input[0].size(); j++) {

                MatrixRotateOutPut[j][Input.size() - 1 - i] = Input[i][j];

            }
        }
        vector<vector<int>> Input(MatrixRotateOutPut.size(), vector<int>(MatrixRotateOutPut[0].size(), 1));
        Input = MatrixRotateOutPut;

        MatrixPrint2D(MatrixRotateOutPut);
        printf("\n");

        Irritation--;
        return MatrixRotateOutPut;

    }
}


int main() {
    
    //Define a matrix for testing:
    vector<vector<int>> Matrix2(4, vector<int>(5, 1));
    int R = 2;
    int C = 4;
    vector<vector<int>> MatrixInput(R, vector<int>(C, 1));;

    for (int i = 0; i < MatrixInput.size(); i++) {
        for (int j = 0; j < MatrixInput[0].size(); j++) {
            int temp;
            temp = i ^ (2 * j);

            MatrixInput[i][j] = temp;
        }
    }

    cout << "MatrixInput:" << endl;
    MatrixPrint2D(MatrixInput);
    printf("\n");

    vector<vector<int>> OutPut2 = MatrixRotate(MatrixInput, 4);

    return 0;
}

Solution

  • You may do the following:

    So:

    vector<vector<int>> MatrixRotate(vector<vector<int>> Input, int Irritation) {
        vector<vector<int>> MatrixRotateOutPut;
        while (Irritation > 0) {
            cout << Irritation << "\n" << endl;
    
            MatrixRotateOutPut = vector<vector<int>>(Input[0].size(), vector<int>(Input.size(), 1));
    
            for (int i = 0; i < Input.size(); i++) {
                for (int j = 0; j < Input[0].size(); j++) {
    
                    MatrixRotateOutPut[j][Input.size() - 1 - i] = Input[i][j];
    
                }
            }
            vector<vector<int>> Input(MatrixRotateOutPut.size(), vector<int>(MatrixRotateOutPut[0].size(), 1));
            Input = MatrixRotateOutPut;
    
            MatrixPrint2D(MatrixRotateOutPut);
            printf("\n");
    
            Irritation--;
        }
        return MatrixRotateOutPut;
    }