简体   繁体   中英

Using array multiple times in C++ Function

I am working on Conway's game of life and am having issues with my arrays. I can pass the 2d array in once to my function that evaluates the current world. 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 1 0 0

0 0 1 0 0

0 0 1 0 0

0 0 0 0 0

turns in to

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

but if it gets passed a second time my results go crazy

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

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 . Change the line:

int worldo[5][5];

to

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

There are 2 problems in your code:

  1. You forget to initialize your temp array: worldo , to 0. Without a memory clear, your array will be filled up with rubbishes. 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} ;

  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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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