简体   繁体   English

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

[英]Using array multiple times in C++ Function

I am working on Conway's game of life and am having issues with my arrays. 我正在研究Conway的生活游戏,并且遇到阵列问题。 I can pass the 2d array in once to my function that evaluates the current world. 我可以一次将2d数组传递给评估当前世界的函数。 The results come back like they should. 结果会如期返回。 Then when passed again I get all kinds of garbage. 然后当再次通过时,我得到各种垃圾。 I am thinking it has to do with memory, but I am not sure how to fix it. 我认为它与内存有关,但是我不确定如何解决它。

0 0 0 0 0 0 0 0 0 0

0 0 1 0 0 0 0 1 0 0

0 0 1 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

turns in to 变成

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

0 1 1 1 0 0 1 1 1 0

0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0

but if it gets passed a second time my results go crazy 但是如果第二次通过的话,我的结果会很疯狂

1946813184 32767 1946813184 32767 1946812520 1946813184 32767 1946813184 32767 1946812520

32767 1946813184 1 1411353440 32767 32767 1946813184 1 1411353440 32767

-1983101020 0 1 0 1411353160 -1983101020 0 1 0 1411353160

32767 1946813184 1 1946815600 32767 32767 1946813184 1 1946815600 32767

1 0 1946813176 32767 1946815600 1 0 1946813176 32767 1946815600

Here is my code 这是我的代码

#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];
        }
    }
}

You forgot to initialize worldo in updateWorld . 您忘记在worldo中初始化updateWorld Change the line: 更改行:

int worldo[5][5];

to

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

There are 2 problems in your code: 您的代码中有2个问题:

  1. You forget to initialize your temp array: worldo , to 0. Without a memory clear, your array will be filled up with rubbishes. 您忘记了初始化临时数组: worldo ,将其初始化为0。如果没有清除内存,您的数组将被填满。 This is because the space for arrays are taken randomly from memory. 这是因为数组的空间是从内存中随机获取的。 There's no guarantee what the initial value is. 不能保证初始值是多少。 That's why you are strongly recommended to set an initial value of a variable , instead of using it directly. 因此,强烈建议您设置变量的初始值 ,而不要直接使用它。 To deal with your error, You could do one of these: memset(worldo, 0, sizeof(worldo)) or int worldo = {0} ; 要处理您的错误,您可以执行以下操作之一: memset(worldo, 0, sizeof(worldo))int worldo = {0} ;

  2. When i+a < 0 , c should equal to 'c = x - 1' instead of c = x , and when j + b < 0 your d should be d = y - 1 . i+a < 0c应该等于'c = x-1'而不是c = x ,并且当j + b < 0 d应该为d = y - 1

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

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