I am trying to move a function named printReport to main and I am having trouble with the parameters... THe parameter "count" when passed is not printing out the correct count in the output. I would really appreciate a hand at getting this to work. Thank you very much.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdio.h>
using namespace std;
const int MAX = 30;
const char TREE = '+';
const char BURNING = '#';
const char BURNEDOUT = '.';
void burnForest(char forest [MAX][MAX], int& probFactor);
void displayForest(char [MAX][MAX]);
void getInput(int& rowTree, int& colTree);
void getProbability(int& probFactor);
void initForest(char forest[MAX][MAX],int row, int column);
void initSim(char forest [MAX][MAX], int& probFactor);
bool isAnyTreeOnFire(char forest [MAX][MAX]);
bool isNeighborOnFire(char forest[MAX][MAX], int row, int column);
void printReport(char forest[MAX][MAX], int probFactor, int burnCycle);
void printRequired246Headings();
void updateForest(char forest [MAX][MAX]);
void wait(int seconds);
|
/*** M A I N */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int main()
{
//Creat 2D Array named Forest
int probFactor = 0;
char forest [MAX][MAX];
//Name, Course Number, and Program Desctiption
printRequired246Headings();
//initializes Simulation by Gathering User Input and setting One Tree on Fire
initSim(forest, probFactor);
//Burn Forest
burnForest(forest, probFactor);
//Print final report
//Required print when program terminates normally
cout <<"\n\n*--Normal Termination--*\n";
system("pause");
return 0;
}
|
/*** initForest ***/
// Initialize Forest with "trees" and sets one on fire
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
void initForest(char forest [MAX][MAX], int row, int column)
{
//Initialize row[0] and row[29]
for (int r = 0; r < MAX; r++)
{
//Initialize column[0] and column[29]
for (int c = 0; c < MAX; c++)
{
if(r == 0 || r == (MAX - 1))
forest[r][c]= ' ';
else if(c == 0 || c == (MAX - 1))
forest[r][c] = ' ';
else
//Initailizes remainder of array
forest[r][c] = TREE;
}//end for column loop
}//end for row loop
//Set start point of burning tree
forest[row][column] = BURNING;
return;
}
|
/*** displpayForest ***/
// Displays the Forest
1 2 3 4 5 6 7 8 9
|
void displayForest(char forest [MAX][MAX])
{
for (int i = 0; i < MAX; i++)
{
cout << "\n";
for (int j = 0; j < MAX; j++)
cout << forest[i][j];
}
}
|
/*** getInput ***/
//Get user input for row and column of tree to start burn
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
void getInput(int& rowTree, int& colTree)
{
//Input from user for selection of row and column for start point
cout << "This program will simulate a forest fire based on user input" << endl;
cout << "\nWhat row is your tree in? Please enter an integer between 1 & 28: ";
cin >> rowTree;
cout << "\nWhat column is your tree in? Please enter an integer between 1 & 28: ";
cin >> colTree;
//Validation loop to check if row is within 1 and 28
while (rowTree <1 || rowTree >MAX - 2)
{
cout << "\nYour tree is not in the forest. Try Again.";
cin >> rowTree;
}
//Validation loop to check if column is within 1 and 28
while (colTree <1 || colTree >MAX - 2)
{
cout << "\nYour tree is not in the forest. Try Again.";
cin >> colTree;
}
}
|
/*** getProbability ***/
//Get Probability from User to determine progression of fire
1 2 3 4 5 6 7 8 9 10 11 12
|
void getProbability(int& probFactor)
{
cout << "\nPlease enter a probability for the forest fire between 1 & 100: ";
cin >> probFactor;
//Validation loop to check if probability is between 1 and 100
while (probFactor <1 || probFactor >100)
{
cout << "\nPlease re-enter a probability between 1 & 100: ";
cin >> probFactor;
}
}
|
/*** isNeighborOnFire ***/
//Function to determmine is neighboring tree is on fire
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
bool isNeighborOnFire(char forest[MAX][MAX], int row, int column)
{
//Set direction to zero
int dir = 0;
bool onFire = false;
//Declare and initialize a Direction Array for the 8 locations around a tree
int dirAry [8][2] = {{-1,-1},{-1,0},{-1,-1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
//This while loop scans through the Direction Array to help determine
//the probability of neighboring trees being on fire
while (dir <8 && !onFire)
{
int r = row + dirAry[dir][0];
int c = column + dirAry[dir][1];
if (forest[r][c] == BURNING)
{
onFire = true;
}
else
{
dir++;
}
}
return onFire;
}
|
/*** voidWait ****/
//This function adds a wait time in between the forest update
//Function modified from cplusplus.com
1 2 3 4 5 6 7 8
|
void wait(double seconds)
{
//Stall "seconds" sec by system clock
clock_t endwait;
endwait = clock () + seconds * CLOCKS_PER_SEC;
while (clock() < endwait)
{/* do nothing */}
}
|
/*** updateForest ****/
//Updates state of forest to determine state of tree, BURNING or
//BURNEDOUT and copies forest to tempForest out to forest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
void updateForest(char forest [MAX][MAX], int probFactor)
{
//Creating temp forest to store state of current forest
char tempForest [MAX][MAX];
for(int r = 0; r < MAX; r++)
for(int c = 0; c < MAX; c++)
tempForest[r][c] = forest[r][c];
//Walk through row to update
for (int r = 1; r < MAX -1; r++)
{
//Walk through column to update
for (int c = 1; c < MAX - 1; c++)
{
if(isNeighborOnFire(forest, r, c))
{
//Uses rand to vs. probFactor to determine which tree will BURN
if ((rand()%100)+1 < probFactor && forest[r][c] == TREE)
tempForest [r][c] = BURNING;
}
if (forest [r][c] == BURNING)
{
tempForest[r][c] = BURNEDOUT;
}
}//end for column loop
}//end for row loop
//copies result back to main forest
for(int r = 0; r < MAX; r++)
for(int c = 0; c < MAX; c++)
forest[r][c] = tempForest[r][c];
}
|
/*** initSim ****/
1 2 3 4 5 6 7 8 9 10 11
|
void initSim(char forest [MAX][MAX], int& probFactor)
{
//Seeding Rand with time
srand((unsigned int)(time(NULL)));
//Calling Functions to run simulation
int rowTree = 0;
int colTree = 0;
getInput(rowTree, colTree);
getProbability(probFactor);
initForest(forest, rowTree, colTree);
}
|
/*** burnForest ****/
//Burns forest, displays forest, updates forest, and
//checks to see if any tree is on fire
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
void burnForest(char forest [MAX][MAX], int& probFactor)
{
int count = 0;
while (isAnyTreeOnFire(forest))
{
displayForest(forest);
updateForest(forest, probFactor);
//counts cycles
count++;
wait(.5);
}
//Print final report
printReport(forest, probFactor, count);
}
|
/*** printReport ****/
//Prints final report of forest, with probability factor, number and percent of trees
//burned and alive.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void printReport(char forest[MAX][MAX], int probFactor, int burnCycle)
{
displayForest(forest);
cout << endl << endl;
//Runs through the Final Forest to count Burned and Alive Trees
int numBurned = 0;
for(int row = 0; row < MAX; row++)
for(int column = 0; column < MAX; column++)
if(forest[row][column] == BURNEDOUT)
numBurned++;
int totalTrees = (MAX -2)*(MAX - 2);
int treeRemaining =(MAX -2)*(MAX - 2) - numBurned;
//Displays the Final Report
cout << "Final Report\n";
cout << "Total Cycles: " << burnCycle << endl;
cout << "Total trees burned: " << numBurned << endl;
//Percentage of Trees Burned
cout << "Percentage of Trees Burned: " << (numBurned / (float)totalTrees) *100 << "%\n";
cout << "Trees remaining: " << treeRemaining << endl;
//Percentage of Trees Remaining
cout << "Percentage of Trees remaining: " << (treeRemaining / (float)totalTrees) *100 << "%\n";
cout << "With a Probability of: " << probFactor << "%\n";
}
|
/** isAnyTreeOnFire ****/
// Checks to see if any tree is on fire is the forest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
bool isAnyTreeOnFire(char forest [MAX][MAX])
{
bool anyOnFire = false;
int i; //Loop control variable
int j; //Loop control variable
// Runs through forest to determine trees on fire and returns forest
for ( i = 0; i < MAX; i++)
{
//cout << "\n";
for ( j = 0; j < MAX; j++)
{
//cout << forest[i][j];
if (forest[i][j] == BURNING)
{
anyOnFire = true;
}
}
}
return anyOnFire;
}
|