顺时针旋转2D阵列
Rotating 2d array clockwise
我需要创建AD 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;
}
}
希望通过"旋转",您并不意味着常规转置,因为在stackoverflow上已经有很多答案。在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
尝试使用此;
for (int i = 0; i <row;i++)
k = row.length -1 ;
for(int j = 0;j<col; j++)
{
Roti[j][k]=arr[i][j];
}
}
相关文章:
- C++动态安全 2D 交错阵列
- 库特<<恩德尔;不适用于打印 2D 阵列
- 具有可变尺寸的 C++ 2D 阵列
- 如何通过Boost.MPI发送2d Boost.MultiArray的子阵列?
- 如何从2d Boost.MultiArray获取子阵列?
- 基于用户输入的 2D 动态阵列
- 2D 动态阵列被视为特洛伊木马?
- 如何遍历 2D 阵列?
- 超级对撞机2D阵列:生成嵌套:包装/尺寸问题
- 动态 2D 阵列.为什么分段错误?
- 尝试使用2D阵列
- 分配给阵列时出现分段错误?黑客排名 2D 阵列 - DS.
- 如何将1D阵列访问为2D阵列
- 分配/访问2d阵列,使得2d子块是连续的
- 重新分配2D阵列并删除旧阵列
- 不使用新的动态2D阵列
- 我正在为2D阵列参数而挣扎
- 特征和巨大的密集 2D 阵列
- 全球字符阵列(2D)
- 如何将内存映像分配给DLIB阵列2d或映像