Thread: Help Needed

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    3

    Help Needed

    I have a program called Life. This program creates an array from the numbers in a .txt file, then it puts it into an array. The cells in the array will have "bacteria" if they are named in the .txt file. The bacteria will be removed if it does not have 2-3 neighbors. The bacteria in an empty cell will be added if it has exactly 3 neighbors.

    My program goes through five "generations". Everything works, but 8 cells. I cant find a flaw in my code or theory, so I am looking for help.

    Also, is there any better way to print out the grid numbers at the top? (the column rows)
    I've tried using setw and set(precision), but the setw will have to many spaces, and the set(precision) only works with "double", but "double" cannot be used in arrays.

    Coordinates that are wrong...

    ••••row,col
    14,12
    17,13
    17,14
    18,12
    18,13
    18,14
    18,15
    19,11
    19,14



    Instructions if comments are not clear enough...

    ••••Background:

    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). The rules for the creation of the next generation are as follows:
    1. Every empty cell with three living neighbors will come to life in the next generation.
    2. Any cell with one or zero neighbors will die of loneliness, while any cell with four or more neighbors will die from overcrowding.
    3. Any cell with two or three neighbors will live into the next generation.
    4. All births and deaths occur simultaneously.
    Assignment:
    1. Write a program that solves the game of Life. The size of the grid will be a square
    20 x 20.
    2. The original grid of bacteria will be supplied to your program from a text file.
    a. The first line will contain the number (N) of bacteria locations in the file.
    b. What follows are N pairs of data, one pair of numbers per line.
    c. The first value of each line indicates the row location while the second value on the line indicates the column location.
    d. The data file values are given as: 1 <= Row <= 20 and 1 <= Col <= 20.
    3. After your program has initialized the grid with generation 0, your program must allow Life to proceed for 5 generations.
    4. Display the final results on the screen and determine the following statistical information:
    a. The number of living cells in the entire board.
    b. The number of living cells in row 10.
    c. The number of living cells in column 10.


    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cassert>
    #include <iomanip>
    
    
    using namespace std;
    
    const int MAX = 21;                                      //creates constant int for the size of the array
    
    void input (int cellGrid[][MAX])                         //takes info from a .txt and puts it into an array
    {
    	ifstream infile;                                     //declare a file variable
    	int row;
    	int column;
    	int number;
    	infile.open("life100.txt");                          //open a file
    	assert (infile);                                     //make sure file opened
    	
    	infile >> number;                                    //inserts number of "bacteria"
    
    	while(infile>>row>>column) {                         //inserts bacteria places into array
    	cellGrid[row][column]=1;                             //makes it equal one if bacteria is present
    	}
          
    	infile.close();                                      //closes file
    }
    
    
    void clearLife1(int finalGrid[][MAX])                    //clears finalGrid
    {
         for(int row=1;row<MAX;row++)                        //goes through each row
         {
                 for(int col=1;col<MAX;col++)                //goes through each column
                 {
                         finalGrid[row][col]=0;              //clears the specific cell
                 }
          }
    }
    
    void clearLife2(int cellGrid[][MAX])                     //clears cellGrid
    {
         for(int row=1;row<MAX;row++)                        //goes through each row
         {
                 for(int col=1;col<MAX;col++)                //goes through each column
                 {
                         cellGrid[row][col]=0;               //clears the specific cell
                 }
          }
    }
    
    
    void cellToFinal (int cellGrid[][MAX], int finalGrid[][MAX])          //changes cellGrid bacteria and moves to finalGrid
    {
         
         int neighborCounter;
         clearLife1 (finalGrid);
         for (int row=1; row<MAX; row++)
         {
             for (int column=1; column<MAX; column++)                     //counts up the neighbors
             {
                 neighborCounter = 0;
                 if (cellGrid [row-1][column] == 1){
    	       	       		neighborCounter++;
        	   	       		}		
                 if (cellGrid [row-1][column+1] == 1){
           	    	   		neighborCounter++;
           	       			}	
                 if (cellGrid [row-1][column-1] == 1){
        	              	neighborCounter++;
           		       		}	
           		if (cellGrid [row][column+1] == 1){
       	    	       		neighborCounter++;
        	   	       		}	
        		if (cellGrid [row][column-1] == 1){
           	    	   		neighborCounter++;
           	       			}	
        		if (cellGrid [row+1][column+1] == 1){
           		       		neighborCounter++;
           	    	   		}	
        		if (cellGrid [row+1][column] == 1){
           		       		neighborCounter++;
           	    	   		}	
        		if (cellGrid [row+1][column-1] == 1){
           		       		neighborCounter++;
           	    	   		}	
                if (cellGrid [row][column] == 1)                                           //if the cell is not empty then...
            	        {
    	                    if (neighborCounter == 2 or neighborCounter == 3)              //if the cell has two or three neighbors then...
           	       			{
           	       			finalGrid [row][column] = 1;                                   //the bacteria lives
           	       			}	
    	  		
    		  			    else if (neighborCounter != 2 and neighborCounter !=3)         //if the cell does not have two or three neighbors then...
        	   	       		{
           		       		finalGrid [row][column] = 0;                                   //the bacteria dies
    					    }
    	      		     }
    					
        		else if (cellGrid [row][column] == 0)                                      //if the cell is empty then...
    	  	    	 	{
       	    	       		if (neighborCounter == 3)                                      //if the cell has three neighbors then...
        	   	       		{
           		       		finalGrid [row][column] = 1;                                   //the cell will grow a "bacteria"   
                	     	}
                	     	
                	     	else if (neighborCounter != 3)                                 //if the cell does not have three neighbors then...
                	     	{
                            finalGrid [row][column] = 0;                                   //the cell will not change
                	     	}
    	  	    	 	}
             }
         }
    }
    
    
    
    void finalToCell (int cellGrid[][MAX], int finalGrid[][MAX])          //changes finalGrid bacteria and moves to cellGrid
    {
         int neighborCounter;
         clearLife2 (cellGrid);
         for (int row=1; row<MAX; row++)
         {
             for (int column=1; column<MAX; column++)                     //counts up the neighbors
             {
                 neighborCounter = 0;
                 if (finalGrid [row-1][column] == 1){
    	       	       		neighborCounter++;
        	   	       		}		
                 if (finalGrid [row-1][column+1] == 1){
           	    	   		neighborCounter++;
           	       			}	
                 if (finalGrid [row-1][column-1] == 1){
        	              	neighborCounter++;
           		       		}	
           		if (finalGrid [row][column+1] == 1){
       	    	       		neighborCounter++;
        	   	       		}	
        		if (finalGrid [row][column-1] == 1){
           	    	   		neighborCounter++;
           	       			}	
        		if (finalGrid [row+1][column+1] == 1){
           		       		neighborCounter++;
           	    	   		}	
        		if (finalGrid [row+1][column] == 1){
           		       		neighborCounter++;
           	    	   		}	
        		if (finalGrid [row+1][column-1] == 1){
           		       		neighborCounter++;
           	    	   		}	
                if (finalGrid [row][column] == 1)                                           //if the cell is not empty then...
            	        {
    	                    if (neighborCounter == 2 || neighborCounter == 3)               //if the cell has two or three neighbors then...
           	       			{
           	       			cellGrid [row][column] = 1;                                     //the bacteria lives
           	       			}	
    	  		
    		  			    else if (neighborCounter < 2 || neighborCounter > 3)            //if the cell does not have two or three neighbors then...
        	   	       		{
           		       		cellGrid [row][column] = 0;                                     //the bacteria will die
    					    }
    	      		     }
    					
        		else if (finalGrid [row][column] == 0)                                      //if the cell is empty then...
    	  	    	 	{
       	    	       		if (neighborCounter == 3)                                       //if the cell has three neighbors then...
        	   	       		{
           		       		cellGrid [row][column] = 1;                                     //the cell will grow a "bacteria"   
                	     	}
                	     	
                	     	else if (neighborCounter != 3)                                  //if the cell does not have three neighbors then...
                	     	{
                	     	cellGrid [row][column] = 0;                                     //the cell will not change
                            }
    					}
    		}
    	}
    }
    
    void switcher (int cellGrid[][MAX], int finalGrid[][MAX])
    {
    	 int switching = 0;
         for (int counter=0; counter<=4; counter++)
         {
             if (switching == 0)
             {
                  cellToFinal (cellGrid, finalGrid);
                  switching ++;
             }
             
             else if (switching == 1)
             {
                  finalToCell (cellGrid, finalGrid);
                  switching --;
             }
         }
    }
    
    
    void work (int cellGrid[][MAX], int finalGrid[][MAX], int &number, int &rowTen, int &columnTen)
    {
         switcher (cellGrid, finalGrid);
         
    
         cout << "   01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20" << endl;
         for (int row=1; row<MAX; row++)
         {
             cout <<setw(2)<< row;
             for (int column=1; column<MAX; column++)
             {
                 cout << " ";
                
                 if (finalGrid[row][column] == 0){
            	    cout << "  ";
                 }
                	
                 else if (finalGrid[row][column] == 1){
               	    cout << " *";
               	    if (column == 10)
               	    {
               	    	columnTen ++;
               	    }
               	    if (row == 10)
               	    {
               	    	rowTen ++;
    				}
               	    number ++;
                 }
             }
         cout << endl;
         }
         
         
         
    }
    
    int main()
    {
    	int number = 0;
    	int columnTen = 0;
    	int rowTen = 0;
    	int cellGrid[MAX][MAX] = {'\0'};              //initial array
    	int finalGrid[MAX][MAX] = {'\0'};             //final array to print out
    	input (cellGrid);                             //inputs items from a .txt file into the array
    	work (cellGrid, finalGrid, number, columnTen, rowTen);           //changes array
    	cout << "\n______________________________" << endl;
    	cout << "The number of bacteria is... " << number << ". " << endl;
    	cout << "The number of bacteria in column ten is..." << columnTen << ". " << endl;
    	cout << "The number of bacteria in row ten is..." << rowTen << ". " << endl;
    	
    	cout << endl;
    	
    	
    	return 0;
    }
    life.txt

    ••••
    100
    1 3
    1 7
    1 8
    1 11
    1 12
    1 17
    2 1
    2 5
    2 8
    3 7
    3 13
    3 16
    3 20
    4 1
    4 5
    4 15
    4 17
    4 18
    5 1
    5 9
    5 13
    5 15
    5 16
    6 1
    6 3
    6 5
    6 8
    6 14
    6 18
    7 12
    7 15
    7 16
    7 19
    8 5
    8 11
    8 14
    8 15
    9 7
    9 12
    9 13
    9 15
    9 18
    9 20
    10 7
    10 9
    10 14
    10 15
    11 4
    11 11
    11 20
    12 7
    12 8
    12 10
    12 11
    12 18
    13 1
    13 3
    13 13
    14 1
    14 2
    14 8
    14 9
    14 15
    14 16
    15 8
    15 10
    15 13
    15 14
    15 17
    15 19
    16 1
    16 3
    16 7
    16 9
    16 11
    16 12
    16 17
    16 18
    17 3
    17 4
    17 19
    18 3
    18 8
    18 12
    18 14
    18 20
    19 1
    19 6
    19 7
    19 12
    19 13
    19 14
    19 15
    20 4
    20 5
    20 7
    20 9
    20 12
    20 17
    20 18

  2. #2
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    I just cant find a flaw in my theory...

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Set a breakpoint for one of the cells that goes wrong. Have it stop your program when that happens.

    Or sprinkle some if() statements around your suspect functions, and have it sleep() for a long time, as soon as it goes bad (so you can study the screen). Then back up when it stops, and rerun it. What suspicious or just wrong values do you see?

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    3
    Let me try that O_O

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. Replies: 15
    Last Post: 12-31-2007, 06:26 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. Who's telling the truth??? Career Advice Needed Badly
    By Ican'tCjax,fl in forum C Programming
    Replies: 1
    Last Post: 11-06-2002, 06:16 PM