Home > front end >  Rotate a 2-D array by 90 degrees
Rotate a 2-D array by 90 degrees

Time:07-30

How can I take the array size input from the user and pass it to the function. I tried #define inside the function, it doesn't work since the array definition needs the array bound at compile time. I tried global variable too, it says to define a integer constant which is not feasible in my case since I want to get the size from the user. How can I solve this issue?

#include <iostream>
using namespace std;

// reverse the transposed matrix as step 2
void reverseColumns(int arr[N][N])
{
    for (int i = 0; i < N; i  )
    {
        for (int j = 0; j < N / 2; j  )
        {
            int temp = arr[i][j];
            arr[i][j] = arr[i][N - j - 1];
            arr[i][N - j - 1] = temp;
        }
    }
}
// take the transpose of matrix as step 1
void transposeMatrix(int arr[N][N])
{
    for (int i = 0; i < N; i  )
    {
        for (int j = i; j < N; j  )
        {
            int temp = arr[i][j];
            arr[i][j] = arr[j][i];
            arr[j][i] = temp;
        }
    }
}

void rotateMatrix(int mat[N][N])
{
    transposeMatrix(mat);
    reverseColumns(mat);
}
// printing the final result
void displayMatrix(int mat[N][N])
{
    int i, j;

    for (i = 0; i < N; i  )
    {
        for (j = 0; j < N; j  )
            cout << mat[i][j] << "\t";
        cout << "\n";
    }

    cout << "\n";
}

int main()
{
    int T, N;
    cin >> T;
    while (T > 0)
    {
        cin >> N;
        int mat[N][N];
        for (int i = 0; i < N; i  )
        {
            for (int j = 0; j < N; j  )
            {
                cin >> mat[i][j];
            }
        }
        int res[N][N];
        rotateMatrix(mat);
        displayMatrix(mat);
    }
    return 0;
}

CodePudding user response:

One way to make it work is get the input of rows and cols from user and make a one dimensional array dynamically. for example: Let ROWS and COLS be the values you got via cin. Then the array can be declared as

int* arr = new int[ROWS * COLS];

Instead of writing arr[i][j] you have to write

arr[i * COLS   j]

Also you have to delete the array using

delete[] arr;

CodePudding user response:

You are using C so you should take advantages of it. As kiner_shah commented the fast way to fix your code is just by use of std::vector<std::vector<int>>.

This is better solution but stil poor:

#include <iostream>
#include <vector>

using namespace std;

using Matrix = std::vector<std::vector<int>>;
Matrix makeSquereMatrix(size_t N)
{
    return {N, std::vector<int>(N)};
}

// reverse the transposed matrix as step 2
void reverseColumns(Matrix& arr)
{
    auto N = arr.size();
    ... // no changes here
}
// take the transpose of matrix as step 1
void transposeMatrix(Matrix& arr)
{
    auto N = arr.size();
    ... // no changes here
}

void rotateMatrix(Matrix& mat)
{
    transposeMatrix(mat);
    reverseColumns(mat);
}
// printing the final result
void displayMatrix(const Matrix& mat)
{
    for (auto& row : mat)
    {
        for (auto x : row)
            cout << x << "\t";
        cout << "\n";
    }

    cout << "\n";
}

void readMatrix(Matrix& m)
{
    for (auto& row : m)
    {
        for (auto& x : row)
        {
            cin >> x;
        }
    }
}

int main()
{
    int T, N;
    cin >> T;
    while (T > 0)
    {
        cin >> N;
        auto mat = makeSquereMatrix(N);
        readMatrix(mat);
        rotateMatrix(mat);
        displayMatrix(mat);
        --T;
    }
    return 0;
}

Live demo

Better solution would be introducing a class containing std:::vector with methods performing required actions.

BTW some time ago I've made some matrix code for C. Here is live demo

  • Related