【发布时间】:2021-12-31 02:06:44
【问题描述】:
我正在尝试实现"follow-the-cycles" 算法以就地转置矩阵。矩阵以行优先顺序存储在数组中。我还使用布尔数组来跟踪访问过的位置。这是我目前所拥有的:
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
void print_matrix(const int *mtx, int rows, int columns)
{
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < columns; ++j)
{
printf("%2d ", mtx[(i * columns) + j]);
}
printf("\n");
}
}
void transpose_matrix(int *mtx, int rows, int columns)
{
int size = rows * columns;
bool *visited = malloc(sizeof (bool) * size);
// ...
free(visited);
}
int main(void)
{
enum { ROWS = 4, COLUMNS = 3 };
int mtx[ROWS * COLUMNS] = {
1, 2, 3,
4, 5, 6,
7, 8, 9,
10, 11, 12
};
printf("Original:\n");
print_matrix(mtx, ROWS, COLUMNS);
printf("\nTransposed:\n");
transpose_matrix(mtx, ROWS, COLUMNS);
print_matrix(mtx, ROWS, COLUMNS);
return 0;
}
我不太了解伪代码的某些部分,所以我无法实现它。
【问题讨论】: