2

I am trying to code a "Conways Game of Life" Visualization. I think I have a solid idea on how to go about it but my issue I am running into is this: When I attempt to output the rows and columns of my 2d array, it starts jumping between numbers towards the end and it never stops scrolling the numbers. It seems to get caught on the "x" of 78.

#include <iostream>
#include <cstring>
#include <cstdlib>
#define HEIGHT 25
#define WIDTH 80
using namespace std;

void makeBoard();
int seed = 0;

int main()
{
    makeBoard();
}

void makeBoard()
{
    int board[79][24] = {0};
    /* Seed the random number generator with the specified seed */
    srand(seed);
    for(int x = 0; x <= 79; x++)
    {
        for(int y = 0; y <= 24; y++)
        {
            /* 50% chance for a cell to be alive */
            if(rand() % 100 < 50)
            {
                board[x][y] = {1};
            }
            else
            {
                board[x][y] = {0};
            }
            /*if(board[x][y] == 1) {
                    cout << "SPAM" << endl;
                     }*/
                     //this is just printing out the current location it is iterating through.
            cout << "X: " << x << " Y: " << y << endl;
        }
        cout << endl;
    }
}

all of the code needed to run it should be right there.

Thank you for your help and patience.

2 Answers 2

6

Your indices are out of bounds. an array of [79][24] has indices going from 0-19, and 0-23. Your condition stop at 79 and 24 respectively. Replace <= with <.

Sign up to request clarification or add additional context in comments.

Comments

0

An array of size N goes from 0 to n-1. You need to replace the <= with <, since you're running out of bounds along each dimension of the array.

Note also that you only have 79 columns and 24 rows, not the 80 and 25 you defined at the top of your program. You can fix this by doing:

int board[HEIGHT][WIDTH];

Then substitute the 79 and 24 with HEIGHT and WIDTH respectively, and change the <= in the loop conditions to <. That way all you need do is change those single values at the top to change the size of the board throughout.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.