Array rotation in C

2019-09-21 05:42发布

I am trying to rotate an array that looks like this:

    a a a a  
    a b a a  
    b b b a
    a a a a

I am supposed to rotate it 5 times for 90 degrees. It is supposed to be done in C.

I appreciate every help because I am just a beginner and am stuck on this.

Thanks in advance.

#include <stdio.h>

int main()
{
    char array_1[4][4] = { {'-','-','-','-'},
                           {'-','o','-','-'},
                           {'o','o','o','-'},
                           {'-','-','-','-'}};

    char array_2[4][4] = { {'-','-','-','-'},
                           {'-','o','o','-'},
                           {'o','o','-','-'},
                           {'-','-','-','-'}};

    char array_3[4][4] = { {'-','-','-','-'},
                           {'-','o','-','-'},
                           {'-','o','-','-'},
                           {'-','o','o','-'}};

    char array_4[4][4] = { {'-','-','o','-'},
                           {'-','-','o','-'},
                           {'-','-','o','-'},
                           {'-','-','o','-'}};

    int counter = 0;
    int counter_1 = 0;


    for(counter = 0; counter < 4; counter++)
    {
        for(counter_1 = 0; counter_1 < 4; counter_1++)
        {
            printf("%c ",array_1[counter][counter_1]);
        }
        printf("  ");

        for(counter_1 = 0; counter_1 < 4; counter_1++)
        {
            printf("%c ",array_2[counter][counter_1]);
        }
        printf("  ");

        for(counter_1 = 0; counter_1 < 4; counter_1++)
        {
            printf("%c ",array_3[counter][counter_1]);
        }
            printf("  ");

        for(counter_1 = 0; counter_1 < 4; counter_1++)
        {
            printf("%c ",array_4[counter][counter_1]);
        }
        printf("  ");

        printf("\n");
    }

    printf("\n");

    for(counter= 0; counter < 4; counter++)
    {
        for(counter_1 =  3; counter_1 >= 0; counter_1--)
        {
            printf("%c ",array_1[counter_1][counter]);
        }
        printf("  ");
        for(counter_1 =  3; counter_1 >= 0; counter_1--)
        {
            printf("%c ",array_2[counter_1][counter]);
        }
        printf("  ");
        for(counter_1 =  3; counter_1 >= 0; counter_1--)
        {
            printf("%c ",array_3[counter_1][counter]);
        }
        printf("  ");
        for(counter_1 =  3; counter_1 >= 0; counter_1--)
        {
            printf("%c ",array_4[counter_1][counter]);
        }
        printf("  ");

        printf("\n");
    }
    printf("\n");

1条回答
Deceive 欺骗
2楼-- · 2019-09-21 06:36

like this:

#include <stdio.h>

typedef struct point { int x, y; } Point;

void rotate(int n, char array[n][n]){
    //rotate right 90 degrees
    if(n == 1) return ;
    int times = n / 2;
    for(int i = 0; i < times; ++i){
        Point base = { i, i };
        for(int j = 0; j < n - 1; ++j){
            Point transition[4] = { {j, n-1}, {n-1,n-1-j},{n-1-j,0},{0,j} };
            char curr = array[base.x][base.y+j];//base + {0,j}
            for(int k = 0; k < 4; ++k){
                char temp = array[base.x + transition[k].x][base.y + transition[k].y];
                array[base.x + transition[k].x][base.y + transition[k].y] = curr;
                curr = temp;
            }
        }
        n -= 2;
    }
}

void display(int n, char array[n][n]){
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            if(j)
                putchar(' ');
            putchar(array[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}

int main(void){
    //demo
    char array4[4][4] = {
        {'1','2','3','4'},
        {'5','6','7','8'},
        {'9','A','B','C'},
        {'D','E','F','0'}
    };
    display(4, array4);
    int n = 4;
    while(n--){
        rotate(4, array4);
        display(4, array4);
    }
    char array5[5][5] = {
        {'A','B','C','D','E'},
        {'F','G','H','I','J'},
        {'K','L','M','N','O'},
        {'P','Q','R','S','T'},
        {'U','V','W','X','Y'}
    };
    display(5, array5);
    n = 4;
    while(n--){
        rotate(5, array5);
        display(5, array5);
    }
}
查看更多
登录 后发表回答