ch1ck3n 0 Newbie Poster
#include <iostream>	// required for input and output
#include <fstream>
#include <cstdlib>	// required for rand() function
using namespace std;

// function prototypes
void storeData(char cellArray[][18]);
void printArray(char cellArray[][18]);
int checkNeighbors(char cellArray[][18], int row, int col);
void makeContagious(char cellArray[][18], int dayArray[][18], int row, int col, int neighbors);

int main()
{
	char cells[49][18];	// stores the status of each cell
	int days[49][18];	// stores the number of days the cell has been contagious or immune
	int numNeighbors;

	

	storeData(cells);	// write status of cells to an array

//	printArray(cells);

	for (int m = 0; m < 49; m++)
	{
		for (int n = 0; n < 18; n++)
		{
			days[m][n] = 0;
		}
	}
	

	for (int day = 1; day <= 14; day++)
	{
		for (int i = 0; i < 49; i++)
		{
			for (int j = 0; j < 18; j++)
			{
				// determine number of infected cells around each cell
				numNeighbors = checkNeighbors(cells, i, j);	
						
				// change status of healthy cells to contagious
				// also set a newly contagious or immune cell's day count to zero
				makeContagious(cells, days, i, j, numNeighbors);


//				cout << cells[i][j] << " ";
				cout << days[i][j] << " ";	// output to see if days is working 
			}
			cout << endl;
		}
		cout << endl;
	}


	return 0;
}

// function to write cell status to an array
void storeData(char cellArray[][18])
{
	ifstream infile;

	infile.open("cells.txt");

	if (infile.fail())
	{
		cout << "The file did not open correctly." << endl;
		return;
	}

	for (int i = 0; i < 49; i++)
	{
		for (int j = 0; j < 18; j++)
		{
			infile >> cellArray[i][j];
		}
	}

	infile.close();
}

// function to output the array
void printArray(char cellArray[][18])
{
	ifstream infile;

	infile.open("cells.txt");
	
	if (infile.fail())
	{
		cout << "The file did not open correctly." << endl;
		return;
	}

	for (int i = 0; i < 49; i++)
	{
		for (int j = 0; j < 18; j++)
		{
			cout << cellArray[i][j] << " ";
		}

		cout << endl;
	}

	infile.close();
}

// function to check the status of surrounding cells
int checkNeighbors(char cellArray[][18], int row, int col)
{
	int infected = 0;

	// only check surrounding cells if cell is healthy
	if (cellArray[row][col] != 'c' && cellArray[row][col] != 'i')
	{
		if ((row - 1) >= 0 && (col - 1) >= 0)	// not outside the array
		{
			if (cellArray[row-1][col-1] == 'c')
			{
				infected += 1;
			}
		}
		if ((row - 1) >= 0)	// not outside the array
		{
			if (cellArray[row-1][col] == 'c')
			{
				infected += 1;
			}
		}
		if ((row - 1) >= 0 && (col + 1) < 18)	// not outside the array
		{
			if (cellArray[row-1][col+1] == 'c')
			{
				infected += 1;
			}
		}
		if ((col - 1) >= 0)	// not outside the array
		{
			if (cellArray[row][col-1] == 'c')
			{
				infected += 1;
			}
		}
		if ((col + 1) < 18)	// not outside the array
		{
			if (cellArray[row][col+1] == 'c')
			{
				infected += 1;
			}
		}
		if ((row + 1) < 49 && (col - 1) >= 0)	// not outside the array
		{
			if (cellArray[row+1][col-1] == 'c')
			{
				infected += 1;
			}
		}
		if ((row + 1) < 49)	// not outside the array
		{
			if (cellArray[row+1][col] == 'c')
			{
				infected += 1;
			}
		}
		if ((row + 1) < 49 && (col + 1) < 18)	// not outside the array
		{
			if (cellArray[row+1][col+1] == 'c')
			{
				infected += 1;
			}
		}
	}

	return (infected);	// return total number of infected neighbors 
}

// function to change the status of each cell depending on the number of neighbors that are infected
void makeContagious(char cellArray[][18], int dayArray[][18], int row, int col, int neighbors)
{
	int chance;
	
	if (cellArray[row][col] != 'h' )	// if cells are contagious or immune
	{
		dayArray[row][col] += 1;			
	}
	
	
	if (neighbors == 1)
	{
		chance = 1 + rand() % 8;	// one in eight chance
			if (chance == 5)
			{
				cellArray[row][col] = 'c';
				dayArray[row][col] = 0;
			}
	}
	if (neighbors == 2)
	{
		chance = 2 + rand() % 8;	// two in eight chance
			if (chance == 5)
			{
				cellArray[row][col] = 'c';
				dayArray[row][col] = 0;
			}
	}
	if (neighbors == 3)
	{
		chance = 3 + rand() % 8;	// three in eight chance
			if (chance == 5)
			{
				cellArray[row][col] = 'c';
				dayArray[row][col] = 0;
			}
	}
	if (neighbors == 4)
	{
		chance = 4 + rand() % 8;	// four in eight chance
			if (chance == 5)
			{
				cellArray[row][col] = 'c';
				dayArray[row][col] = 0;
			}
	}
	if (neighbors == 5)
	{
		chance = 5 + rand() % 8;	// five in eight chance
			if (chance == 5)
			{
				cellArray[row][col] = 'c';
				dayArray[row][col] = 0;
			}
	}
	if (neighbors == 6)
	{
		chance = 6 + rand() % 8;	// six in eight chance
			if (chance == 5)
			{
				cellArray[row][col] = 'c';
				dayArray[row][col] = 0;
			}
	}
	if (neighbors == 7)
	{
		chance = 7 + rand() % 8;	// seven in eight chance
			if (chance == 5)
			{
				cellArray[row][col] = 'c';
				dayArray[row][col] = 0;
			}
	}
	// if 8 neighbors are infected, the cell with turn contagious
	if (neighbors == 8)	
	{
		cellArray[row][col] = 'c';
		dayArray[row][col] = 0;
	}
	
}

My Problem:
First of all, I cant use pointers for this program...

For some reason, when i run this program with the for loop in the main function commented, it runs fine, i get my desired output. But then when i try uncomment the for loop (run it 14 times) it changes my output, the numbers change to different numbers even on the lines that should be the same.

I dont understand why it would run perfectly one time through, but when i try to run through it more than once, i get unwanted results.

Initial Input File found at >>> http://www.cse.psu.edu/~cg103/handouts/cells.txt

What the program does:

emply dimensional arrays to mimic the spread of a disease through an array of cells.. cells are either "Contagious, Healthy, or Immune".. A healthy cell is not contagious but can get the disease, a contagious cell spreads the disease, and an immune cell is infected but will no longer spread the disease

the spread of the disease should be monitored on a daily basis. Each day status of all the cells should be evaluated.

A healthy cell may be infected based on how many of its neighbors are contagious.. If one neighbor is infected, there is a one in 8 chance the healthy cell will become contagious... 2 neighbors infected: 2 in 8 chance, etc. if 8 neighbors are infected, the healthy cell will definitly become contagious

A newly infected cell is given a day count of zero, once a cell is infected, it remains infected for 5 days, then becomes immune, A newly immune cell stays that way for 7 days, then becomes healthy again (and able to be reinfected)

The original status of the cells is in cells.txt, you can assume that all cells have a day count of 0


This file is working fine in Borland, but wont work in Visual, and unfortuanatly i have to use Visual.

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.