简体   繁体   中英

Rotate 90 degrees 2D char array

I can't really see what the issues on this code are ?

I'm trying for instance to get:

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

from:

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

Here is my code:

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;
}

Is it an issue with the syntax ? It seems to work fine with integers...

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;
}                                                                      

As pointed out by @Some programmer dude, there are two main issues:

First, you do not allocate a "2D-array" in the sense of an "nxn matrix of characters", but you seem to allocate an array of n pointers, each pointing to a sequence of n characters. So you should allocate n pointers in the first place, ie char **array = malloc(n * sizeof(char*)) .

Second, you repeatedly exceed array bounds with the use of i <= n throughout; use i < n instead. BTW: you did not declare i and j ...

Having fixed this, your program works:

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;
}

I would suggest the temporary table. It will be easier to understand the code

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;
}

You have many instances of accessing the array beyond its boundaries: for (i = 0; i <= n; i++) should be changed to

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

Also i is not even defined...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM