Hey guys!

I guess my other post wasn't so clear...probably because I wasn't sure what exactly I was trying to do.

Anyway, right now I am having problems reading my input file into my 20x20 2D array. I mean, it compiles fine. But, I have a print function just to see if anything was put into the array, and all I see are 0's.
Below I will post my code and the contents of the input file.
Any help, as always, is appreciated.

This is what is in my input file:
0 0
0 5
0 9
0 19
1 6
1 14
1 16
2 4
2 7
2 11
2 14
2 15
2 16
2 18
3 2
3 5
3 9
3 10
3 17
3 19
4 0
4 3
4 5
4 9
5 2
5 3
5 5
5 15
5 16
5 17
6 5
6 6
6 10
6 11
6 13
6 14
6 17
6 18
7 6
7 8
7 14
7 16
8 1
8 5
8 8
8 10
8 17
9 3
9 4
9 6
9 7
10 0
10 9
10 19
11 1
11 2
11 3
11 5
11 7
11 8
11 14
11 17
11 19
12 4
12 5
12 6
12 8
13 0
13 1
13 7
13 10
13 11
13 14
13 15
13 16
13 17
13 18
14 2
14 11
14 13
14 15
14 17
14 19
15 11
15 13
15 18
16 1
16 2
16 5
16 8
16 17
17 5
17 6
18 6
18 16
18 18
19 0
19 3
19 4
19 7

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

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

int main()
{
    int bacteria[ROW][COL];

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

//*************************************************************************
void readLife(int bacteria[][COL])
{
     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][col];
     infile>>row>>col;
     }
     infile.close();
}

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

//****************************************************************************
void printLife(int bacteria[][COL])
{
     int col;
     
      for(col=0;col<COL;col++)
      {
       cout<<bacteria[5][col]<<'\n';
       }
}

Is a 2D array the best choice here? You have more than one value corresponding to each "row" number?

infile>>row>>col;
    
     while(infile)
     {
     bacteria[row][col];
     infile>>row>>col;
     }
     infile.close();

should probably be something like

int i=0;
while(infile>>row>>column)
{
           bacteriaX[i] = row;
           bacteriaY[i] = column;
           i++;
}

My assignment says I MUST use a 2D array.

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.

So yeah, I have no choice unfortunately...

OK, I gotcha now. Well you still need to assign something to the bacteria[row][col] for it to show up in your array.

while(infile>>row>>column)
           bacteria[row][col] = 1;

will work. But as part of your design considerations you need to decide what you want those cells to be -- is integer best, or boolean? In other words what will "store' the state of the system the best between iterations.

Good point. I actually first tried to make my array of type bool but the second I got an error, I went back to int. :$
However, I will try some more tweaking and I'll be right back. Thanks for the pointers!

OK, I gotcha now. Well you still need to assign something to the bacteria[row][col] for it to show up in your array.

while(infile>>row>>column)
           bacteria[row][col] = 1;

will work. But as part of your design considerations you need to decide what you want those cells to be -- is integer best, or boolean? In other words what will "store' the state of the system the best between iterations.

Hey there! Thanks again for the suggestions. Okay, so I changed over my array to type bool. And I also changed my print function. I am at the point where I am simply displaying the contents of the file. Everything looks perfect except no * shows up at row 0 column 0. It's kinda hard to explain but I'll post my code and my output(the output might be kind of messed up due to formatting but just so u can see what i'm talking about). Please help! Once I'm past this hurdle, I think I may be able to fly through the rest(*crosses fingers*)

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

const int ROW=20;
const int COL=20;
void readLife(bool bacteria[][COL]);
void initLife(bool bacteria[][COL]);
void printLife(bool bacteria[][COL]);

int main()
{
    bool bacteria[ROW][COL];

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

//*************************************************************************
void readLife(bool bacteria[][COL])
{
     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>>row>>col)
     {
     bacteria[row][col]=true;
     }
     infile.close();
}

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

//****************************************************************************
void printLife(bool bacteria[][COL])
{   
      cout << "  01234567890123456789" << endl;
      for (int row=0;row<ROW;row++)
      {
          cout <<setw(2)<< row;
          for (int col=0;col<COL;col++)
          {
            if (bacteria[row][col])
            {
                cout<<"*";
            } 
            else 
            {
                cout<<" ";
            }
          }
           cout<<endl;
        }
      

}

01234567890123456789
0 * * *
1 * * *
2 * * * *** *
3 * * ** * *
4* * * *
5 ** * ***
6 ** ** ** **
7 * * * *
8 * * * * *
9 ** **
10* * *
11 *** * ** * * *
12 *** *
13** * ** *****
14 * * * * * *
15 * * *
16 ** * * *
17 **
18 * * *
19* ** *
Press any key to continue . . .

Get rid of infile>>row>>col; on line 39, it's reading in 0,0 and throwing it out.

Oh wow...I didn't even see that I had that in there twice. Thanks!

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.