逆时针旋转 2d Array 元素 k 次 | C语言

Rotate 2d Array elements counter clockwise k times | C language

我是 C 编程语言的新手。我不明白问题在说什么,一周都无法解决。我创建了一个用 C 语言逆时针旋转矩阵元素的程序。当我 运行 它时,我有两个问题出来了。以下是问题:

In function 'main':
matrix-rotation.c:13:16: warning: passing argument 1 of 'gridRotate' makes pointer from integer without a cast [-Wint-conversion]
     gridRotate(arr[ROW][COL], 1);
                ^~~
matrix-rotation.c:6:5: note: expected 'int (*)[4]' but argument is of type 'int'
 int gridRotate(int grid[ROW][COL], int k);

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#define ROW 4
#define COL 4

int gridRotate(int grid[ROW][COL], int k);
int main() {
    int arr[ROW][COL] = {{1, 2, 3, 4},
                         {5, 6, 7, 8},
                         {9, 10, 11, 12},
                         {13, 14, 15, 16}};
    gridRotate(arr[ROW][COL], 1);
    return 0;
}

int gridRotate(int grid[ROW][COL], int k){
    int rst=0, ren=ROW-1, cst=0, cen=COL-1;
    int p1, p2, p3, p4;
    int tempK =k;
    for (; rst < ren && cst < cen; rst++, ren--, cst++, cen--)
    {
        tempK = k % (((ren - rst) * 2 +(cen - cst)) * 2);
        for (int t = 0; t < tempK; t++)
        {
            p1 = grid[rst][cst];
            p2 = grid[ren][cst];
            p3 = grid[ren][cen];
            p4 = grid[rst][cen];
            for (int i = ren - 1; i > rst; i--)
                grid[i + 1][cst] = grid[i][cst];
            for (int i = cen - 1; i > cst; i--)
                grid[ren][i + 1] = grid[ren][i];
            for (int i = rst+1 ; i < ren; i++)
                grid[i - 1][cen] = grid[i][cen];
            for (int i = cst+1; i < cen; i++)
                grid[rst][i - 1] = grid[rst][i];

            grid[rst + 1][cst] = p1;
            grid[ren][cst + 1] = p2;
            grid[ren - 1][cen] = p3;
            grid[rst][cen - 1] = p4;
        }
    }
    for(int i=0; i<ROW; i++){
        for(int j=0; j<COL; j++){
            printf("%d  ",grid[i][j]);
        }
        printf("\n");
    }
}

正如我在 中指出的那样,您需要将函数调用编写为:

 gridRotate(arr, 1);

而不是:

 girdRotate(arr[ROW][COL], 1);

这是工作代码(扩展问题中的 MCVE):

/* SO 7023-1056 */
#include <stdio.h>
#include <stdlib.h>

static void gridRotate(int rows, int cols, int grid[rows][cols], int k);
static void dump_matrix(const char *tag, int rows, int cols, int matrix[rows][cols]);

int main(void)
{
    enum { ROWS1 = 4, COLS1 = 4 };
    int matrix1[ROWS1][COLS1] =
    {
        {  1,   2,   3,   4 },
        {  5,   6,   7,   8 },
        {  9,  10,  11,  12 },
        { 13,  14,  15,  16 },
    };
    dump_matrix("Initial", ROWS1, COLS1, matrix1);
    gridRotate(ROWS1, COLS1, matrix1, 1);
    dump_matrix("Rotated", ROWS1, COLS1, matrix1);

    enum { ROWS2 = 6, COLS2 = 5 };
    int matrix2[ROWS2][COLS2] =
    {
        { 623, 237, 103, 925, 197, },
        { 611, 166, 352, 108, 815, },
        { 375, 417, 579, 641, 777, },
        { 251, 346, 290, 869, 803, },
        { 769, 598, 116, 721, 122, },
        { 514, 734, 256, 314, 817, },
    };
    dump_matrix("Initial", ROWS2, COLS2, matrix2);
    gridRotate(ROWS2, COLS2, matrix2, 1);
    dump_matrix("Rotated 1", ROWS2, COLS2, matrix2);
    gridRotate(ROWS2, COLS2, matrix2, 2);
    dump_matrix("Rotated 2", ROWS2, COLS2, matrix2);
    gridRotate(ROWS2, COLS2, matrix2, 3);
    dump_matrix("Rotated 3", ROWS2, COLS2, matrix2);

    enum { ROWS3 = 5, COLS3 = 6 };
    int matrix3[ROWS3][COLS3] =
    {
        /* random -n 30 100 999 | commalist -w -n 6 -B 8 -b '{ ' -T ' },' */
        { 627, 877, 154, 366, 198, 910, },
        { 981, 611, 122, 176, 695, 427, },
        { 525, 111, 850, 549, 850, 668, },
        { 775, 919, 350, 209, 547, 379, },
        { 740, 534, 340, 220, 216, 166, },
    };
    dump_matrix("Initial", ROWS3, COLS3, matrix3);
    gridRotate(ROWS3, COLS3, matrix3, 1);
    dump_matrix("Rotated 1", ROWS3, COLS3, matrix3);
    gridRotate(ROWS3, COLS3, matrix3, 2);
    dump_matrix("Rotated 2", ROWS3, COLS3, matrix3);
    gridRotate(ROWS3, COLS3, matrix3, 3);
    dump_matrix("Rotated 3", ROWS3, COLS3, matrix3);

    enum { ROWS4 = 6, COLS4 = 6 };
    int matrix4[ROWS4][COLS4] =
    {
        /* random -n 36 100 999 | commalist -w -n 6 -B 8 -b '{ ' -T ' },' */
        { 846, 371, 941, 122, 553, 122, },
        { 224, 748, 174, 315, 751, 771, },
        { 689, 709, 141, 502, 952, 151, },
        { 994, 411, 293, 289, 712, 740, },
        { 171, 427, 403, 850, 292, 296, },
        { 803, 312, 880, 268, 403, 423, },
    };
    dump_matrix("Initial", ROWS4, COLS4, matrix4);
    gridRotate(ROWS4, COLS4, matrix4, 1);
    dump_matrix("Rotated 1", ROWS4, COLS4, matrix4);
    gridRotate(ROWS4, COLS4, matrix4, 2);
    dump_matrix("Rotated 2", ROWS4, COLS4, matrix4);
    gridRotate(ROWS4, COLS4, matrix4, 4);
    dump_matrix("Rotated 3", ROWS4, COLS4, matrix4);

    return 0;
}

static void gridRotate(int rows, int cols, int grid[rows][cols], int k)
{
    int cst = 0;
    int ren = rows - 1;
    int cen = cols - 1;
    for (int rst = 0; rst < ren && cst < cen; rst++, ren--, cst++, cen--)
    {
        int tempK = k % (((ren - rst) * 2 + (cen - cst)) * 2);
        for (int t = 0; t < tempK; t++)
        {
            int p1 = grid[rst][cst];
            int p2 = grid[ren][cst];
            int p3 = grid[ren][cen];
            int p4 = grid[rst][cen];
            for (int i = ren - 1; i > rst; i--)
                grid[i + 1][cst] = grid[i][cst];
            for (int i = cen - 1; i > cst; i--)
                grid[ren][i + 1] = grid[ren][i];
            for (int i = rst + 1; i < ren; i++)
                grid[i - 1][cen] = grid[i][cen];
            for (int i = cst + 1; i < cen; i++)
                grid[rst][i - 1] = grid[rst][i];
            grid[rst + 1][cst] = p1;
            grid[ren][cst + 1] = p2;
            grid[ren - 1][cen] = p3;
            grid[rst][cen - 1] = p4;
        }
    }
}

static void dump_matrix(const char *tag, int rows, int cols, int matrix[rows][cols])
{
    printf("%s (%dx%d):\n", tag, rows, cols);
    for (int i = 0; i < rows; i++)
    {
        const char *pad = "";
        for (int j = 0; j < cols; j++)
        {
            printf("%s%2d", pad, matrix[i][j]);
            pad = "  ";
        }
        printf("\n");
    }
}

输出:

Initial (4x4):
 1   2   3   4
 5   6   7   8
 9  10  11  12
13  14  15  16
Rotated (4x4):
 2   3   4   8
 1   7  11  12
 5   6  10  16
 9  13  14  15
Initial (6x5):
623  237  103  925  197
611  166  352  108  815
375  417  579  641  777
251  346  290  869  803
769  598  116  721  122
514  734  256  314  817
Rotated 1 (6x5):
237  103  925  197  815
623  352  108  641  777
611  166  579  869  803
375  417  290  721  122
251  346  598  116  817
769  514  734  256  314
Rotated 2 (6x5):
925  197  815  777  803
103  641  869  721  122
237  108  579  116  817
623  352  290  598  314
611  166  417  346  256
375  251  769  514  734
Rotated 3 (6x5):
777  803  122  817  314
815  116  598  346  256
197  721  579  417  734
925  869  290  166  514
103  641  108  352  769
237  623  611  375  251
Initial (5x6):
627  877  154  366  198  910
981  611  122  176  695  427
525  111  850  549  850  668
775  919  350  209  547  379
740  534  340  220  216  166
Rotated 1 (5x6):
877  154  366  198  910  427
627  122  176  695  850  668
981  611  850  549  547  379
525  111  919  350  209  166
775  740  534  340  220  216
Rotated 2 (5x6):
366  198  910  427  668  379
154  695  850  547  209  166
877  176  850  549  350  216
627  122  611  111  919  220
981  525  775  740  534  340
Rotated 3 (5x6):
427  668  379  166  216  220
910  209  350  919  111  340
198  547  850  549  611  534
366  850  695  176  122  740
154  877  627  981  525  775
Initial (6x6):
846  371  941  122  553  122
224  748  174  315  751  771
689  709  141  502  952  151
994  411  293  289  712  740
171  427  403  850  292  296
803  312  880  268  403  423
Rotated 1 (6x6):
371  941  122  553  122  771
846  174  315  751  952  151
224  748  502  289  712  740
689  709  141  293  292  296
994  411  427  403  850  423
171  803  312  880  268  403
Rotated 2 (6x6):
122  553  122  771  151  740
941  751  952  712  292  296
371  315  293  141  850  423
846  174  289  502  403  403
224  748  709  411  427  268
689  994  171  803  312  880
Rotated 3 (6x6):
151  740  296  423  403  268
771  850  403  427  411  880
122  292  293  141  709  312
553  712  289  502  748  803
122  952  751  315  174  171
941  371  846  224  689  994