旋转90度2D char数组

时间:2017-09-16 20:05:22

标签: c arrays memory multidimensional-array rotation

我真的看不出这段代码的问题是什么?

我正试图获得:

w 3 l l  
d o n 3
g a m 3
o v 3 r 

来自:

l 3 3 r
l n m 3
3 0 4 v
w d g 0

这是我的代码:

int main() {
    printf("Size of array : ");
    int n;
    scanf("%d", &n);
    printf("Cases : \n");
    char **array = (char**)malloc(n * sizeof(char));
    for (i = 0; i < n; i++)
        array[i] = (char*)malloc(n * sizeof(char));
    int j;
    // Words input
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n; j++) {
            scanf("%c", &array[i][j]);
        }
    }
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n; j++) {
            printf("%c", array[i][j]);
        }
    }
    printf("\n\n");

    // Transpose of the array
    for (i = 0; i <= n; i++) {
        for (j = i + 1; j < n; j++) {
            char tmp = array[i][j];
            array[i][j] = array[j][i];
            array[j][i] = tmp;
        }
    }

    // Display the transposed array
    printf("Transposed array \n");
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n; j++) {
            printf("%c", array[i][j]);
        }
    }
    printf("\n");
    printf("\n");
    // Swap the columns
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n / 2; j++) {
            char tmp = array[i][j];
            array[i][j] = array[i][n - 1 - j];
            array[i][n - 1 - j] = tmp;
        }
    }
    // Display after rotation
    printf("After rotation\n");
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n; j++) {
            printf("%c", array[i][j]);
        }
    }
    printf("\n");
    return 0;
}

这是语法问题吗? 它似乎与整数一起正常工作......

int main() {
    printf("Size of array : ");
    int n;
    scanf("%d", &n);
    printf("Cases : \n");
    char **array = (char**)malloc(n * sizeof(char));
    for (i = 0; i < n; i++)
        array[i] = (char*)malloc(n * sizeof(char));
    int j;
    // Words input
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n; j++) {
            scanf("%c", &array[i][j]);
        }
    }
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n; j++) {
            printf("%c", array[i][j]);
        }
    }
    printf("\n\n");
    // Transpose of the array
    for (i = 0; i <= n; i++ ) {
        for (j = i + 1; j < n; j++ ) {
            char tmp = array[i][j];
            array[i][j] = array[j][i];
            array[j][i] = tmp;
        }
    }
    // Display the transposed array
    printf("Transposed array \n");
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n; j++) {
            printf("%c", array[i][j]);
        }
    }
    printf("\n");
    printf("\n");
    // Swap the columns
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n / 2; j++) {
            char tmp = array[i][j];
            array[i][j] = array[i][n - 1 - j];
            array[i][n - 1 - j] = tmp;
        }
    }
    // Display after rotation
    printf("After rotation\n");
    for (i = 0; i <= n; i++) {
        for (j = 0; j < n; j++) {
            printf("%c", array[i][j]);
        }
    }
    printf("\n");
    return 0;
}                                                                      

3 个答案:

答案 0 :(得分:1)

正如@Some程序员老兄所指出的,有两个主要问题:

首先,你没有分配一个&#34; 2D阵列&#34;在&#34; n x n字符矩阵&#34;的意义上,你似乎分配了一个n个指针数组,每个指针指向一个n个字符的序列。所以你应该首先分配n个指针,即char **array = malloc(n * sizeof(char*))

其次,您在整个过程中使用i <= n重复超出数组范围;请改用i < n。顺便说一句:您没有声明ij ...

解决了这个问题后,你的程序就可以了:

int main()
{
    printf("Size of array : ");
    int n;
    scanf("%d", &n);
    printf("Cases : \n");
    char **array = malloc(n * sizeof(char*));
    for (int i = 0; i < n; i++)
        array[i] = malloc(n * sizeof(char));
    int j;
    // Words input
    for (int i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            scanf("%c", &array[i][j]);
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
    printf("\n\n");

    // Transpose of the array
    for (int i = 0; i < n; i++ )
    {
        for (int j = i + 1; j < n; j++ )
        {
            char tmp = array[i][j];
            array[i][j] = array[j][i];
            array[j][i] = tmp;
        }
    }

    // Display the transposed array
    printf("Transposed array \n");
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    printf("\n");
    // Swap the columns
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n / 2; j++)
        {
            char tmp = array[i][j];
            array[i][j] = array[i][n - 1 - j];
            array[i][n - 1 - j] = tmp;
        }
    }
    // Display after rotation
    printf("After rotation\n");
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%c", array[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    return 0;
}

答案 1 :(得分:0)

我会建议临时表。理解代码会更容易

char **rotate(char **table, size_t size)
{
    char *tmp = malloc(sizeof(char) * size * size);

    if (tmp != NULL)
    {
        for (size_t col = 0; col < size; col++)
            for (size_t row = size; row; row--)
            {
                size_t rr = row - 1;
                tmp[size - row + col * size] = table[rr][col];
            }
        for (size_t row = 0; row < size; row++)
        {
            memcpy(table[row], tmp + row * size, sizeof(char) * size);
        }
        free(tmp);
    }
    return tmp == NULL ? NULL : table;
}

答案 2 :(得分:0)

您有许多超出其边界访问数组的实例:List<E> subList(int fromIndex, int toIndex)应更改为

for (i = 0; i <= n; i++)

for (i = 0; i < n; i++) 甚至没有定义......