Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的数组旋转_C_Arrays_Rotation - Fatal编程技术网

C语言中的数组旋转

C语言中的数组旋转,c,arrays,rotation,C,Arrays,Rotation,我正在尝试旋转一个如下所示的数组: a a a a a b a a b b b a a a a a #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

我正在尝试旋转一个如下所示的数组:

    a a a a  
    a b a a  
    b b b a
    a a a a
#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);
    }
}
我应该把它旋转5次90度。它应该用C来完成

我很感激每一个帮助,因为我只是一个初学者,我被困在这个问题上

提前谢谢

#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");
#包括
int main()
{
字符数组_1[4][4]={{{'-','-','-','-','-'},
{'-',o','-','-','-'},
{'o','o','o','-'},
{'-','-','-','-'}};
字符数组_2[4][4]={{{'-','-','-','-','-'},
{'-',o',o','-'},
{'o','o','-','-'},
{'-','-','-','-'}};
字符数组_3[4][4]={{{'-','-','-','-','-'},
{'-',o','-','-','-'},
{'-',o','-','-','-'},
{'-','o','o','-'};
字符数组_4[4][4]={{{'-','-','o','-},
{'-','-',o','-'},
{'-','-',o','-'},
{'-','-',o','-'};
int计数器=0;
int计数器_1=0;
用于(计数器=0;计数器<4;计数器++)
{
用于(计数器_1=0;计数器_1<4;计数器_1++)
{
printf(“%c”,数组_1[计数器][计数器_1]);
}
printf(“”);
用于(计数器_1=0;计数器_1<4;计数器_1++)
{
printf(“%c”,数组_2[计数器][计数器_1]);
}
printf(“”);
用于(计数器_1=0;计数器_1<4;计数器_1++)
{
printf(“%c”,数组_3[计数器][计数器_1]);
}
printf(“”);
用于(计数器_1=0;计数器_1<4;计数器_1++)
{
printf(“%c”,数组_4[计数器][计数器_1]);
}
printf(“”);
printf(“\n”);
}
printf(“\n”);
用于(计数器=0;计数器<4;计数器++)
{
用于(计数器\u 1=3;计数器\u 1>=0;计数器\u 1--)
{
printf(“%c”,数组_1[计数器_1][计数器]);
}
printf(“”);
用于(计数器\u 1=3;计数器\u 1>=0;计数器\u 1--)
{
printf(“%c”,数组_2[计数器_1][计数器]);
}
printf(“”);
用于(计数器\u 1=3;计数器\u 1>=0;计数器\u 1--)
{
printf(“%c”,数组_3[计数器_1][计数器]);
}
printf(“”);
用于(计数器\u 1=3;计数器\u 1>=0;计数器\u 1--)
{
printf(“%c”,数组_4[计数器_1][计数器]);
}
printf(“”);
printf(“\n”);
}
printf(“\n”);
像这样:

    a a a a  
    a b a a  
    b b b a
    a a a a
#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);
    }
}
#包括
typedef结构点{intx,y;}点;
无效旋转(整数n,字符数组[n][n]){
//向右旋转90度
如果(n==1)返回;
整数倍=n/2;
对于(int i=0;i
请显示您到目前为止所写的内容。请显示所需的结果。好的,您添加的原始编码似乎可以为测试的4个阵列完成工作。那么,您到底需要什么?我需要将其旋转5次,每次旋转后都会崩溃,因此我不知道我做错了什么。很抱歉,缺乏知识,但这是我第一次编程在C。