NitaB 0 Junior Poster in Training

Hey everyone!

Sooo, I'm working on this program that's basically a practice on using 2D arrays. I am going through this step by step so first I just want to be sure that my program is reading the file and putting the information into the correct places in my array.(It compiles with no problem but my printLife function doesn't really tell me much.)

Below is the assignment and my code thus far. I also have an example output.

The game of life is a computer simulation of the life and death events of a population of organisms. This program will determine the life, death, and survival of bacteria from one generation to the next, assuming the starting grid of bacteria is generation zero (0). Each cell has a total of up to 8 neighbors, including the 4 immediately adjecent cells and the 4 diagonal cells. The rules for the creation of each cell in the next generation are as follows:

If the cell is currently empty:

If the cell has exactly three living neighbors, it will come to life in the next generation.

If the cell has any other number of living neighbors, it will remain empty.

If the cell is currently living:

If the cell has one or zero living neighbors, it will die of loneliness in the next generation.

If the cell has four or more living neighbors, it will die of overcrowding in the next generation.

If the cell has two or three neighbors, it will remain living.

All births and deaths occur simultaneously (make sure you don't get this one wrong!).
Your task is to write a program that plays the game of life. The size of the grid will be a 20 x 20 square. Your solution must use a 20 X 20 2-dimensional array.
Don't declare a bigger array! (It is permissible, of course, to use a second array of the same size if you find it convenient to do so).

The original grid of bacteria will be supplied to your program from a text file. Thetext file will contain one line of data for each bacteria in the original grid. Each linewill consist of a pair of numbers, separated by a space. The first number will indicate the row location of the bacteria and the second number will indicate the column location of the bacteria. Every number in the text file will be between 0 and 19.

After your program has initialized the grid with generation 0, your program must allow life to proceed for 5 generations. Your program should then display the final results on the screen, using a star (*) to represent a live bacteria, along with the following statistical information:
The number of living cells in the entire board.
The number of living cells in row 10.
The number of living cells in column 10.
Please make sure that your output matches the following correct output exactly.
An input file (input.dat) has been posted, along with the correct output and the output including intermediate generations (output.doc). You should turn in a copy of your source code, along with the output produced by your program when run with the input file provided.

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <iomanip>
using namespace std;

const int NUM=20;
void readLife(int bacteria[][NUM]);
void initLife(int bacteria[][NUM]);
void printLife(int bacteria[][NUM]);

int main()
{
    int bacteria[NUM][NUM];

    
    readLife(bacteria);
    printLife(bacteria);
    
    
    system("pause");
    return 0;
    
}

//*************************************************************************
void readLife(int bacteria[][NUM])
{
     ifstream infile;    //declare a file variable
     int row;
     int col;
     
    
     infile.open("input.dat");     //open a file
     assert(infile);               //make sure file opened
     
     initLife(bacteria);           //initialize 2D array to 0
     infile>>row>>col;
    
     while(infile)
     {
     bacteria[row-1][col-1]++;
     infile>>row>>col;
     }
     infile.close();
}

//***************************************************************************
void initLife(int bacteria[][NUM])
{
     for(int row=0;row<NUM;row++)
     {
             for(int col=0;col<NUM;col++)
             {
                     bacteria[row][col]=0;
             }
     }
}

//****************************************************************************
void printLife(int bacteria[][NUM])
{
     cout<<"01234567890123456789"<<endl;
     for(int count=0;count<NUM;count++)
     {
      cout<<bacteria[count][count];
      }
}

I should get an output that looks something like this:
01234567890123456789
0* * * *
1 * * *
2 * * * *** *
3 * * ** * *
4* * * *
5 ** * ***
6 ** ** ** **
7 * * * *
8 * * * * *
9 ** **
10* * *
11 *** * ** * * *
12 *** *
13** * ** *****
14 * * * * * *
15 * * *
16 ** * * *
17 **
18 * * *
19* ** *


The above printLife function was just for me to see if the program was reading my file at all...

So, I guess my main question at this point is: am I getting and storing the information into my 2D array correctly?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.