顺时针旋转2D阵列

时间:2019-02-20 06:22:56

标签: c++ arrays algorithm for-loop dynamic

我需要创建2d动态数组,然后创建一个函数来接收2d动态数组,然后将其顺时针旋转90度,然后将其返回到main。 但是我不确定为什么我没有得到任何输出?是因为交换技术错误? 我认为索引交换如下:

I J-----------I J
0 0           0 1
0 1           1 1
0 2           2 1
我随之而来的

for (int i = 0; i <row;i++)

    for(int j = 0;j<col; j++)
    {
        Roti[i+j][row-i]=arr[i][j];

    }

代码:

#include <iostream>
using namespace std;
void print2DArray(int **arr, int rows, int cols);
int **Rotate(int **arr, int row, int col)
{
    int **Roti = new int *[row];

    for (int i = 0; i <row;i++)
    {
        Roti[i] = new int [col];
    }


    for (int i = 0; i <row;i++)

        for(int j = 0;j<col; j++)
        {
            Roti[i+j][row-i]=arr[i][j];

        }
    return  Roti;
}

int main()
{
    int *A[3];
    for (int i = 0; i < 3; i++)
    {
        A[i] = new int[3];
        for (int j = 0; j < 3; j++)
        {
            A[i][j] = rand() % 20;
        }
    }
    cout << "The array is :\n";
    print2DArray(A, 3, 3);
    int **ptr;
    ptr=Rotate(A,3,3);
cout<<"-----"<<endl;
    print2DArray(ptr, 3, 3);




    for (int i = 0; i < 3; i++)
    {
        delete[] A[i];
    }
    return 0;
}
void print2DArray(int **arr, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

}

2 个答案:

答案 0 :(得分:1)

希望“旋转”不是指常规的转置,因为在stackoverflow上已经有很多答案。 What is the fastest way to transpose a matrix in C++?

为旋转矩阵,我修改了代码以使其工作,如下所示。逻辑很简单,就是原始矩阵的第一行成为旋转矩阵的最后一列,并使用此逻辑,您可以得到以下代码。希望能帮助到你。

#include <iostream>
using namespace std;

void print2DArray(int **arr, int rows, int cols);
int **Rotate(int **arr, int num_rows, int num_cols);

int main()
{
    int num_rows = 3;
    int num_cols = 3;
    int** A_matrix = new int*[num_rows];
    for (int i = 0; i < num_rows; i++)
        A_matrix[i] = new int[num_cols];

    for (int row = 0; row < num_rows; row++)
    {
        for (int col = 0; col < num_cols; col++)
        {
            A_matrix[row][col] = rand() % 20;
        }
    }

    cout << "The array is :\n";
    print2DArray(A_matrix, 3, 3);

    int** roated_matrix;
    roated_matrix = Rotate(A_matrix, num_rows, num_cols);
    cout << "Rotated array:" << endl;
    print2DArray(roated_matrix, 3, 3);


    return 0;
}

int **Rotate(int **arr, int num_rows, int num_cols)
{
    /* Rotated matrix will have dimensions reverse as well. 
       That's why we have rows and cols reversed in the following lines */
    int** rotated_matrix = new int*[num_cols];
    for (int i = 0; i < num_cols; i++)
        rotated_matrix[i] = new int[num_rows];


    for (int row = 0; row < num_rows; row++)
    {
        int col_rotated_matrix = num_rows - 1 - row; // 1st row shall be copied to the last column and so on
        for (int col = 0; col < num_cols; col++)
        {
            int row_rotated_matrix = col; // rows become columns in the rotated matrix
            rotated_matrix[row_rotated_matrix][col_rotated_matrix] = arr[row][col];
        }
    }
    return rotated_matrix;
}
void print2DArray(int **arr, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

它产生以下输出:

The array is :
1 7 14
0 9 4
18 18 2
Rotated array:
18 0 1
18 9 7
2 4 14

答案 1 :(得分:0)

尝试使用它;

for (int i = 0; i <row;i++)
k = row.length -1 ;
    for(int j = 0;j<col; j++)
    {
        Roti[j][k]=arr[i][j];

    }
}