在c中的数组中使用模式时,打印结果很奇怪

时间:2018-12-01 17:14:06

标签: c arrays function

我创建了一个数组和一些函数,每个函数均打印不同的模式。我的问题是它们以怪异的方式打印在阵列内部。下面是代码的主要部分,仅是示例的一个函数。

int castle(int patternWidth, int doorStart, int doorEnd, int N, int i, int j, int row, int col, char** array) 
    {
        if (N >= 3 && N <= 20) 
        {
            for (i = 1; i <= (N + 1); i++) 
            {
                array[row][col] = '*'; col++;
                array[row][col] = ' '; col++;
            }
            row++;

            patternWidth = (((N + 1) * 2) - 1);
            doorStart = (patternWidth - 3) / 2; 
            doorEnd = doorStart + 3; 


            for (i = 0; i < N; i++) 
            { 
                for (j = 1; j <= patternWidth; j++) 
                { 
                    if(N - i <= 2 && j > doorStart && j <= doorEnd)
                    {
                        array[row][col] = ' '; col++;
                    } 
                    else 
                    { 
                        array[row][col] = '*'; col++;
                    }
                }
                row++;
            }
        }
        return 0;
    }

    int main() 
        {
            int N = 0, M = 0, i = 0, j = 0, a = 0, b = 0, s = 0, width = 0, height = 0, patternWidth = 0, doorStart = 0, doorEnd = 0, option = 0, num = 3, col = 0, row = 0;
            char** array;

            printf("Give height board size:");
            scanf("%d", &height);
            printf("Give width board size:");
            scanf("%d", &width);

            array = (char**)malloc(height * sizeof(char*));
            for (i = 0; i < width; i++) 
            {
                array[i] = (char*)malloc(height * sizeof(char));
            }


            for (i = 0; i < height; i++) 
            {
                for (j = 0; j < width; j++) 
                {
                    array[i][j] = ' ';
                }
            }

            while (option != 6)
            {
                printf("\nOption: 1-5, 6 to exit\n"
                "1) Stairs and flag\n"
                "2) Castle\n"
                "3) Trap door\n"
                "4) Platform\n"
                "5) Obstacles\n"
                "Option:");
                scanf("%d", &option);
                if (option == 1) 
                {
                    printf("Valid values 6 - 20\n");
                    printf("Size:");
                    scanf("%d", &N);
                    printf("Height Position:");
                    scanf("%d", &row);
                    printf("Width Position:");
                    scanf("%d", &col);
                    stairs_flag(N, i, j, b, a, num, col, row, array);
                }
                else if (option == 2) 
                {
                    printf("Valid values 3 - 15\n");
                    printf("Size:");
                    scanf("%d", &N);
                    printf("Height Position:");
                    scanf("%d", &row);
                    printf("Width Position:");
                    scanf("%d", &col);
                    castle(patternWidth, doorStart, doorEnd, N, i, j, row, col, array);
                }
                else if (option == 3) 
                {
                    printf("Valid values 3 - 18\n");
                    printf("Size of N:");
                    scanf("%d", &N);
                    printf("Height Position:");
                    scanf("%d", &row);
                    printf("Width Position:");
                    scanf("%d", &col);
                    trap_door(N, patternWidth, i, j, col, row, array);
                }
                else if (option == 4) 
                {
                    printf("Valid values of N 3 - 20\n");
                    printf("Size of N:");
                    scanf("%d", &N);
                    printf("Valid values of M 6, 8, 10, 12, 14, 16, 18, 20\n");
                    printf("Size of M");
                    scanf("%d", &M);
                    printf("Height Position:");
                    scanf("%d", &row);
                    printf("Width Position:");
                    scanf("%d", &col);
                    platform(N, M, i, j, col, row, array);
                }
                else if (option == 5) 
                {
                    printf("Valid values 2 - 10\n");
                    printf("Size of N:");
                    scanf("%d", &N);
                    printf("Height Position:");
                    scanf("%d", &row);
                    printf("Width Position:");
                    scanf("%d", &col);
                    obstacles(N, i, s, j, patternWidth, row, col, array);
                }

            print_array(array, height, width);
            }       

            free(array);

            return 0;
        }

预期结果和正确的结果是这样:

 |------------------------------------------------------------|
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |          * * * * *                                         |
 |          *********                                         |
 |          *********                                         |
 |          ***   ***                                         |
 |          ***   ***                                         |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |------------------------------------------------------------|

但是实际结果是这样:

 |------------------------------------------------------------|
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |          * * * * *                                         |
 |                    *********                               |
 |                             *********                      |
 |                                      ***   ***             |
 |                                               ***   ***    |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |                                                            |
 |------------------------------------------------------------|

例如,在上面的两个阵列中,木板的宽度为60,高度为20,对于图案,我将高度和宽度的位置都设置为10。模式的每一行都应该在前一行之下,并打印一个模式,但实际上,它在前一行之下打印每一行,但比前一行更多。我该如何纠正?功能代码是否有任何错误。对于每个功能,我都做相同的事情。我将array[row][col] = '*'; col++;标记为星号,将array[row][col] = ' '; col++;标记为空格,并将row++;标记为新行。

谢谢您的时间

1 个答案:

答案 0 :(得分:1)

问题似乎是您的列索引col在整个castle()函数中一直在增加。如果希望星号彼此对齐,则需要在for循环开始时将列索引设置回其原始值。

例如,您可以做到

int castle(int patternWidth, int doorStart, int doorEnd, int N, int i, int j, int row, int col, char** array) 
    { 
        int column_index = col;
        if (N >= 3 && N <= 20) 
        {
            for (i = 1; i <= (N + 1); i++) 
            {
                array[row][col] = '*'; col++;
                array[row][col] = ' '; col++;
            }
            row++;

            patternWidth = (((N + 1) * 2) - 1);
            doorStart = (patternWidth - 3) / 2; 
            doorEnd = doorStart + 3; 


            for (i = 0; i < N; i++) 
            { 
                for (j = 1; j <= patternWidth; j++) 
                { 
                    col = column_index;
                    if(N - i <= 2 && j > doorStart && j <= doorEnd)
                    {
                        array[row][col] = ' '; col++;
                    } 
                    else 
                    { 
                        array[row][col] = '*'; col++;
                    }
                }
                row++;
            }
        }
        return 0;
    }

我在其中添加了一个命令int column_index = col;,该命令将原始值存储在函数的开头,在行col = column_index;的内部for循环的开头,将存储的值放回变量。