c - 在 c 中的数组中使用时,模式被打印得很奇怪

标签 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++; 用于在新行中打印每一行。

谢谢你的时间

最佳答案

问题似乎是您的列索引 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 循环,将存储的值放回变量中。

关于c - 在 c 中的数组中使用时,模式被打印得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53573212/

相关文章:

c - 如何在 CPP 中串联宏

c - 如何避免 C 文件名中的 "\"问题?

c - 递归函数崩溃

检查 ISBN 以查看其在 C 语言中是否有效

Javascript:返回长 CR 分隔列表作为变量

javascript - 结合过滤器和包含过滤多个值

python - 在循环内逐列修改numpy数组

jquery 通过名称调用函数

c - 为什么我的角色功能会崩溃?

c++ - 如果条件太长并且包括 for 循环