我该如何为这段代码编写函数

时间:2015-06-02 15:25:44

标签: c loops output

我正在尝试为代码编写函数或方法。我真的迷失了如何使用循环打印输出的数字和数字在下面的输出中死亡。

这是我的代码:

#include <stdio.h>

char grid[10][10];

// Moved this function here
int occ(int x, int y) {
    int i, j, count;
    char gridCopy[10][10];


// You probably are going to do this often in the future
// Make a function for it instead of having the same code often
void printGrid() {
    int i, j;
    for (i = 0; i < 10; i++) {
        for (j = 0; j < 10; j++)
            printf("%c", grid[i][j]);
        printf("\n");
    }
}

2 个答案:

答案 0 :(得分:0)

计数类似于打印或复制整个网格所需的操作:使用相同的循环,但在每个单元格中,根据其中的内容添加到适当的计数器。

或者,如果保持死区和活细胞数的计数,则可以在更新网格时更新这些细胞:如果细胞发生更改,则在细胞变为活着或死亡时适当更新计数器。如果您需要报告出生人数和数量,也可以使用此功能。每回合有多少人死亡。

答案 1 :(得分:0)

gridCopy函数中不需要occ。 请记住,数组是转置的。当您访问元素(x,y)时,您需要使用grid[y][x]

下面是函数generateNext()的工作代码,它为整个grid数组生成下一个状态。这是您需要girdCopy的地方。结果与您的示例完全相同。

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

char grid[10][10];
int  born,died,generation;

// Moved this function here
int occ(int x, int y) {
    int  count;
    int  xm1,xp1,ym1,yp1;

    xm1=x-1;xp1=x+1;ym1=y-1;yp1=y+1;
    if (xm1<0) // handle boundary cases - wrap around
        xm1=9;
    if (ym1<0)
        ym1=9;
    if (xp1>9)
        xp1=0;
    if (yp1>9)
        yp1=0;

    // Checking on the value of the neighboring cells
    count = 0;
    if (grid[ym1][x] == '*')
        count++;
    if (grid[ym1][xp1] == '*')
        count++;
    if (grid[y][xp1] == '*')
        count++;
    if (grid[yp1][xp1] == '*')
        count++;
    if (grid[yp1][x] == '*')
        count++;
    if (grid[yp1][xm1] == '*')
        count++;
    if (grid[y][xm1] == '*')
        count++;
    if (grid[ym1][xm1] == '*')
        count++;

    return count;
}

void generateNext()
{
    int x,y;
    char gridCopy[10][10];

    born=0;died=0;
    generation++;

    for (y=0; y<10; y++) {
        for (x=0; x<10; x++) {
            gridCopy[y][x]=grid[y][x];
        }
    }

    for (y=0; y<10; y++) {
        for (x=0; x<10; x++) {
            if (grid[y][x]=='*' && occ(x,y)<2)
                {gridCopy[y][x]='-';died++;}
            if (grid[y][x]=='*' && occ(x,y)>3)
                {gridCopy[y][x]='-';died++;}
            if (grid[y][x]=='-' && occ(x,y)==3)
                {gridCopy[y][x]='*';born++;}
        }
    }

    for (y=0; y<10; y++) {
        for (x=0; x<10; x++) {
            grid[y][x]=gridCopy[y][x];
        }
    }
}

// You probably are going to do this often in the future
// Make a function for it instead of having the same code often
void printGrid() {
    int x, y;
    for (y = 0; y < 10; y++) {
        for (x = 0; x < 10; x++)
            printf("%c", grid[y][x]);
        printf("\n");
    }
}


/*
 * 
 */
int main(int argc, char** argv) {

    int x, y, answ;

    // Setting entire grid to '-'
    for (y = 0; y < 10; y++)
        for (x = 0; x < 10; x++)
            grid[y][x] = '-'; // No need for a variable, just use '-' directly

    // Printing out grid
    printf("This is the original array\n");
    printGrid();

    // Setting initial state
    grid[5][4] = '*'; // No need for a variable, just use '*' directly
    grid[5][5] = '*'; 
    grid[5][6] = '*'; 
    grid[4][4] = '*'; grid[3][4] = '*';
    grid[4][6] = '*'; grid[3][6] = '*';

    // Printing out grid
    printf("This is the new array\n");
    printGrid();

    //printf("The number of neighbors is: %d\n", occ(3, 3));

    generation=0;
    answ=1;

    while(answ)
    {
        generateNext();
        printf("\n\nGeneration number %d",generation);
        printf("\nBorn=%d,Died=%d\n",born,died);
        printGrid();
        printf("\nPrint next generation-1,Exit-0:");
        scanf("%d",&answ);
    }

    return (EXIT_SUCCESS);
}