I've compiled a working program, for whatever reason, is returning 0 values for the output. I'm not sure why this is happening. I've been troubleshooting this for hours and I can't figure it out. Any help would be much appreciated.
Here is the full program's code:
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
// Global Constant Variables
const int ROWS = 13;
const int COLS = 14;
// Function Prototypes
void initializeArray(int array[][COLS]);
void execute(int array[][COLS], int, int, int);
int move(int array[][COLS], int&, int&, int);
void update(int array[][COLS], int&, int&, int, int&, int&, int&);
double calculateCost(int);
void print(int, int, double, ofstream&);
int main()
{
ofstream outfile;
outfile.open("results.txt");
int water = 0, path = 0, flower = 0;
int array[ROWS][COLS];
initializeArray(array); // populate the array
execute(array, flower, water, path); // run the simulation
double average = calculateCost(flower); // calculate the average costs
print(water, path, average, outfile); // output to file the results
outfile.close();
system("pause");
return 0;
}
// Functions
// Function: Initializes the array
void initializeArray(int array[ROWS][COLS])
{
for(int i = 0; i < ROWS; i++)
{
for(int j = 0; j < COLS; j++)
{
// water
if((i == 0 || i == 12) || ((j == 0 || j == 13) && i != 6))
{
array[i][j] = -1;
}
// path
else if (i == 6 && j != 0 && j != 13)
{
array[i][j] = 0;
}
// bridge
else if (i == 6 && (j == 0 || j == 13))
{
array[i][j] = 3;
}
// flowers
else
{
array[i][j] = 2;
}
}
}
}
// Function: Runs the simulation
void execute(int array[ROWS][COLS], int flower, int water, int path)
{
for(int i = 0; i < 20; i++)
{
initializeArray(array); // reset array
int row = 6, col = 0; // reset position
int position = array[row][col]; // places Harvey in the correct spot
while(position != 3 && position != -1)
{
position = move(array, row, col, position);
update(array, row, col, position, flower, water, path);
}
}
}
// Function: Moves Harvey and determines which direction he stepped
int move(int array[ROWS][COLS], int& row, int& col, int position)
{
int step = rand() % 100 + 1;
if(step <= 47) // forward step (47%)
{
position = array[row][++col];
}
else if (step <= 67) // left step (20%)
{
position = array[--row][col];
}
else if (step <= 92) // right step (25%)
{
position = array[++row][col];
}
else if (step <= 100) // backward step (8%)
{
position = array[row][--col];
}
return position;
}
// Function: Determines if Harvey stepped on a flower
void update(int array[ROWS][COLS], int& row, int& col, int position, int& flower, int& water, int& path)
{
if (position == 2 || position == 1)
{
// removes a flower
array[row][col] -= 1;
flower++;
}
if (position == -1)
{
water++;
}
if (position == 3)
{
path++;
}
}
// Function: Calculates the total costs for flowers stepped on
double calculateCost(int flower)
{
return double((flower * 5.0) / 100);
}
// Function: Outputs results to file
void print(int water, int path, double average, ofstream& outfile)
{
cout << "\n The number of times Harvey was rescued from the water is: " << water
<< "\n\n The number of times Harvey made it to the bridge is: " << path
<< fixed << showpoint << setprecision(2) << "\n\n The walk cost Harvey: $"
<< average << endl;
outfile << "\n The number of times Harvey was rescued from the water is: " << water
<< "\n\n The number of times Harvey made it to the bridge is: " << path
<< fixed << showpoint << setprecision(2) << "\n\n The walk cost Harvey: $"
<< average << endl;
}