
March 24th, 2005, 06:42 PM
|
|
Registered User
|
|
Join Date: Mar 2005
Posts: 2
Time spent in forums: 1 h 12 m 35 sec
Reputation Power: 0
|
|
|
Disease Spread
Code:
#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;
}
}
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.
the initial input file can be found at:
http://www.cse.psu.edu/~cg103/handouts/cells.txt
sorry for the long code
|