简体   繁体   English

将数字添加到网格C

[英]Adding numbers to a grid C

So I am relatively new to C and have been practicing questions on it. 因此,我对C还是比较陌生,并且一直在对此进行练习。 So I came across the question, 'Where can I go?': http://www.chegg.com/homework-help/questions-and-answers/c-coding-java-c--c-coding-bouncy-string-tasked-write-program-take-parameters-command-line--q19411844 所以我遇到了一个问题:“我可以去哪里?”: http : //www.chegg.com/homework-help/questions-and-answers/c-coding-java-c--c-coding-bouncy-字符串任务写入程序通吃参数的命令行- q19411844

I manage to write a part of the code, but I am not sure how to get this 我设法编写了一部分代码,但是我不确定如何获得此代码

| | | |
| | | |
| | | |
| |X| |

I somewhat have the logic of when the counter for width = x - 1, counter for width = x + 1, counter for height = y - 1 (basically the first few steps that can be moved to from the starting position, X) the numbers placed there would be limit - 1. 我有点逻辑,当宽度的计数器= x-1,宽度的计数器= x + 1,高度的计数器= y-1(基本上可以从起始位置X移到的前几步)放置的数字将有限制-1。

Same for when it goes on for i less than or equal to limit the spaces between the limit - 1, will now be filled with limit - 2. So it will be like x-1 && y-1, y-2, y-1 && x+1, x+2 and so on until the last where 0 is placed. 同样,当i持续小于或等于限制-1之间的空格时,现在将充满限制-2。因此它将类似于x-1 && y-1,y-2,y- 1 && x + 1,x + 2,依此类推,直到最后一个放置0的位置。

But how do you write this and fix it in the for loop? 但是,您如何编写此代码并将其修复在for循环中呢?

    #include <stdio.h>
    #include <stdint.h>
    #include <string.h>

    int main()
    {
        int width, height, x, y, max;

        printf("Enter width: ");
        scanf("%d\n", &width);

        printf("Enter height: ");
        scanf("%d\n", &height);

        printf("Enter x: ");
        scanf("%d\n", &x);

        printf("Enter y: ");
        scanf("%d\n", &y);

        printf("Enter walking limit: ");
        scanf("%d\n", &max);


            int b = 0, a = 0;  // initalize local variable

            for (a = 0; a < height; a++) {

                    // fill the width
                    for (b = 0; b < width + 1; b++ ) {

                        printf("|");

                        if (b==x && a==y)
                            {
                                printf("X");
                            }else if(((b==x-1) && (a==y)) || ((b==x+1)  && (a==y)) || ((a==y-1) && (b==x)))
                                {
                                    printf("%d", max-1);
                                }else if(//i am not sure if this will be needed)
                        {
                            for(int i = 2; i <= max; i++)
                            {
                                //add max - i to spaces after C until max is over. 
                            }
                        }
                     }
                            printf("\n");
            }
            return 0; 
    }

You should split your code in several functions. 您应该将代码拆分为几个函数。

I have done a quick example below. 我在下面做了一个简单的例子。

First we need a function to display the array. 首先,我们需要一个函数来显示数组。 You need to create your array, see the malloc. 您需要创建数组,请参见malloc。 Then you set the max steps at the correct position. 然后将最大步数设置在正确的位置。 Then you need a function to do the propagation, this is a very naive way to do. 然后,您需要一个函数来进行传播,这是一种非常幼稚的方式。 Scan the array and set to the value minus one all the cell around. 扫描阵列并将其设置为减去所有周围单元格的值。 Do it max times. 做到最大次数。

For example: 例如:

Enter width: 9
Enter height: 9
Enter x: 3
Enter y: 7
Enter walking limit: 3
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | |0| | | | | |
| | |0|1|0| | | | |
| |0|1|2|1|0| | | |
|0|1|2|3|2|1|0| | |
| |0|1|2|1|0| | | |

The code 编码

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>



static void array_print(int *array, int width, int height)
{

    for (int i = 0; i < height; i++) {
        printf("|");
        for (int j = 0; j < width; j++) {
            int val = array[i * width + j];
            if (val < 0) {
                printf(" |");
            } else {
                printf("%d|", val);
            }
        }
        printf("\n");
    }
}


/**
 *
 * Update the array if x, y is inside the limit and
 * if val is greater than the already set value
 *
 */
static void
array_upate(int *array, int width, int height, int x, int y, int val) {

    if (x >= 0 && x < width && y >= 0 && y < height) {
        int prev = array[y * width + x];
        if (val > prev) {
            array[y * width + x] = val;
        }
    }

}

static void array_propagate(int *array, int width, int height)
{

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int val = array[i * width + j];
            if (val > 0) {
                array_upate(array, width, height, j, i - 1, val - 1);
                array_upate(array, width, height, j, i + 1, val - 1);
                array_upate(array, width, height, j - 1, i, val - 1);
                array_upate(array, width, height, j + 1, i, val - 1);
            }
        }
    }
}

int main()
{
    int width, height, x, y, max;
    int *array;

    printf("Enter width: ");
    scanf("%d", &width);

    printf("Enter height: ");
    scanf("%d", &height);

    printf("Enter x: ");
    scanf("%d", &x);

    printf("Enter y: ");
    scanf("%d", &y);

    printf("Enter walking limit: ");
    scanf("%d", &max);

    /* allocate the array */
    array = malloc(sizeof(int) * width * height);

    /* Set all to -1 */
    memset(array, -1, sizeof(int) * width * height);

    /* set the start to max */
    array_upate(array, width, height, x, y, max);
    /* do the propagation */
    while (max-- > 0) {
        array_propagate(array, width, height);
    }
    array_print(array, width, height);
    free(array);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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