在C ++函数中多次使用数组

时间:2014-08-17 04:38:53

标签: c++ arrays 2d conways-game-of-life

我正在研究康威的生活游戏,并且我的阵列存在问题。我可以将二维数组一次传递给我的函数来评估当前世界。结果会像他们应该的那样回归。然后再次通过我得到各种垃圾。我认为这与记忆有关,但我不确定如何解决它。

0 0 0 0 0

0 0 1 0 0

0 0 1 0 0

0 0 1 0 0

0 0 0 0 0

转入

0 0 0 0 0

0 0 0 0 0

0 1 1 1 0

0 0 0 0 0

0 0 0 0 0

但是如果第二次传递我的结果会发疯

1946813184 32767 1946813184 32767 1946812520

32767 1946813184 1 1411353440 32767

-1983101020 0 1 0 1411353160

32767 1946813184 1 1946815600 32767

1 0 1946813176 32767 1946815600

这是我的代码

#include <iostream>
#include <string>
#include <ctype.h>
#include <cstring>
#include <stdlib.h>


using std::cout;
using std::cin;
using std::endl;
using std::string;

void updateWorld(int world[][5], int x, int y);
int choice();

int main() {
    int world[5][5] = {{ 0, 0, 0, 0, 0},
                        { 0, 0, 1, 0, 0},
                        { 0, 0, 1, 0, 0},
                        { 0, 0, 1, 0, 0},
                        { 0, 0, 0, 0, 0}};


    updateWorld(world, 5, 5); //Call function first time
        for (int i = 0; i < 5; i++) {    //print array
           cout << endl;
            for (int j = 0; j < 5; j++) {
                cout << world[i][j] << " ";
            }
        }

    updateWorld(world, 5, 5); //Call function second time
        for (int i = 0; i < 5; i++) {    //print array
            cout << endl;
            for (int j = 0; j < 5; j++) {
                cout << world[i][j] << " ";
            }
        }


    return 0;
    }

bool validNum(string str) {
    bool valid = true;
    for (int i = 0; i < str.length() && valid == true; i++) {
        valid = isdigit(str.at(i));
    }
    return valid;
}

void updateWorld(int worldi[][5], int x, int y) {
    int worldo[5][5];
    for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) { //counts through all the cells
            int life = 0;            // keeps track of the life around the cell
            for (int a = -1; a < 2; a++) {
                for (int b = -1; b < 2; b++) { //these two loops check every neighbor cell
                    int c = a;
                    int d = b;
                    if (i+a < 0) {
                        c = x;
                    } else if (i + a > x-1) {
                        c = 0;
                    } else {
                        c = i + a;
                    }
                    if (j+b < 0) {
                        d = y;
                    } else if (j+b > y-1){
                        d = 0;
                    } else {
                        d = j + b;
                    }

                    if (worldi[c][d] == 1) {
                        life++;
                      //   << ":" << life << endl;
                    } // cout << c << "," << d << ":" << life << endl;
                }
            }
            life = life - worldi[i][j]; // subtract the cells self value
            if (worldi[i][j] == 1 && life < 2) { // implent the 4 rules
                worldo[i][j] = 0;
            } else if (worldi[i][j] == 1 && 1 < life && life < 4) {
                worldo[i][j] = 1;
            } else if (worldi[i][j] == 1 && life > 3) {
                worldo[i][j] = 0;
            } else if (worldi[i][j] == 0 && life == 3) {
                worldo[i][j] = 1;
            }
        }

    }
    for (int i = 0; i < x; i++) { //set the input arrary to the temp output array
        for (int j = 0; j < y; j++) {
            worldi[i][j] = worldo[i][j];
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您忘记在worldo中初始化updateWorld。改变这一行:

int worldo[5][5];

int worldo[5][5] = {0};

答案 1 :(得分:2)

您的代码中存在两个问题:

  1. 您忘记初始化临时数组:worldo,为0.如果没有内存清除,您的数组将被垃圾填满。这是因为数组的空间是从内存中随机获取的。无法保证初始值是多少。这就是为什么强烈建议您设置变量的初始值,而不是直接使用它。要处理您的错误,您可以执行以下操作之一:memset(worldo, 0, sizeof(worldo))int worldo = {0};

  2. i+a < 0时,c应该等于'c = x - 1'而不是c = x,当j + b < 0d应该d = y - 1是{{1}}。

相关问题