My program takes numbers from a .txt file then puts it in, so that an array has a "bacteria" in it. The "bacteria" will die if it has 4 or more, or if it has 1 or less. It will live with 2-3 neighbors. If an empty cell has three neighbors, then a "bacteria" will grow and live there. I must go through five "days" of dying and growing.

I do this by moving this from one array to the next.

For some reason, I get the wrong answer for the "days"
The array is 20x20, ignoring the 0 lines.

I've tried moving it to multiple arrays and different ways, but 4 hours in, it either was worse or horrible T.T

#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include <iomanip>

using namespace std;

const int MAX = 21;

void input (int cellGrid[][MAX])
{
	ifstream infile; //declare a file variable
	int row;
	int column;
	infile.open("life100[1].txt"); //open a file
	assert(infile); //make sure file opened
	infile>>row>>column;

	while(infile>>row>>column) {
	cellGrid[row][column]=1;
	}
      
	infile.close();
}


void cellToFinal (int cellGrid[][MAX], int finalGrid[][MAX])
{
     
     int neighborCounter;
     for (int row=1; row<MAX; row++)
     {
         for (int column=1; column<MAX; column++)
         {
             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 (neighborCounter == 2 or neighborCounter == 3)
       	       			{
       	       			finalGrid [row][column] = 1;
       	       			}	
	  		
		  			    else if (neighborCounter < 2 or neighborCounter > 3)
    	   	       		{
       		       		finalGrid [row][column] = 0;
					    }
	      		     }
					
    		else if (cellGrid [row][column] == 0)
	  	    	 	{
   	    	       		if (neighborCounter == 3)
    	   	       		{
       		       		finalGrid [row][column] = 1;
            	     	}
   		   		     }
         }
     }
}



void finalToCell (int cellGrid[][MAX], int finalGrid[][MAX])
{
     int neighborCounter;
     for (int row=1; row<MAX; row++)
     {
         for (int column=1; column<MAX; column++)
         {
             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 (neighborCounter == 2 || neighborCounter == 3)
       	       			{
       	       			cellGrid [row][column] = 1;
       	       			}	
	  		
		  			    else if (neighborCounter < 2 || neighborCounter > 3)
    	   	       		{
       		       		cellGrid [row][column] = 0;
					    }
	      		     }
					
    		else if (finalGrid [row][column] == 0)
	  	    	 	{
   	    	       		if (neighborCounter == 3)
    	   	       		{
       		       		cellGrid [row][column] = 1;
            	     	}
   		   		     }
         }
     }
}



















void switcher (int cellGrid[][MAX], int finalGrid[][MAX])
{
     int counter = 0;
     for (int counter=0; counter<=6; counter++)
     {
         if (counter == 0)
         {
              cellToFinal (cellGrid, finalGrid);
              counter ++;
         }
         
         else if (counter == 1)
         {
              finalToCell (cellGrid, finalGrid);
              counter --;
         }
     }
}


void work (int cellGrid[][MAX], int finalGrid[][MAX])
{
     switcher (cellGrid, finalGrid);
     
     
     
     for (int row=1; row<MAX; row++)
     {
         cout.setf(ios::fixed);  
         cout<< setprecision(1) << row;
         for (int column=1; column<MAX; column++)
         {
             cout << " ";
            
             if (finalGrid[row][column] == 0){
        	    cout << " ";
             }
            	
             else if (finalGrid[row][column] == 1){
           	    cout << "*";
             }
         }
     cout << endl;
     }
     
     
     
}

int main()
{
	int cellGrid[MAX][MAX] = {'\0'};
	int finalGrid[MAX][MAX] = {'\0'};
	input (cellGrid);
	work (cellGrid, finalGrid);
	
	return 0;
}

>> For some reason, I get the wrong answer for the "days"

What's "the wrong answer". What output do you expect, and what output are you getting?
And stop bumping your thread, people will help you when they have the time. Bumping is considered rude and will ultimately result in no-one wanting to help you.

This article has been dead for over six months. Start a new discussion instead.