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;
}

I've only skimmed over your code so there could be things I've missed. At first glance:

In your execute function, you need to pass flower, water and path by reference, otherwise the variable values declared at line 25 will never change within the scope of main()

You should also verify …

at lines 78. 79

int row = 6, col = 0;
int position = array[row][col];
// at this point position value = 3

Therefore lines 81 - 85 won't do anything.

while(position != 3 && position != -1)
{
position = move(array, row, …

## All 6 Replies

I've only skimmed over your code so there could be things I've missed. At first glance:

In your execute function, you need to pass flower, water and path by reference, otherwise the variable values declared at line 25 will never change within the scope of main()

You should also verify whether your outfile is open before trying to write to it.

I tried passing by reference, but still the output returns 0 where there should be values. The outfile is definitely open. I checked the results.txt and they show the same 0's as the output to display.

at lines 78. 79

int row = 6, col = 0;
int position = array[row][col];
// at this point position value = 3

Therefore lines 81 - 85 won't do anything.

while(position != 3 && position != -1)
{
position = move(array, row, col, position);
update(array, row, col, position, flower, water, path);
}

Edit: Sorry the "island" isn't looking exactly as I'd have hoped. It should be squared off as a 13x14 rectangle.

I changed:

int row = 6, int col = 0;

To:

int row = 6, int col = 1;

Which puts "Harvey" in a 0 position and will not go against the while condition. However, output is still giving 0 values.

So, the "island" basically looks like this:

-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1
3 0 0 0 0 0 0 0 0 0 0 0 0 3
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1
-1 2 2 2 2 2 2 2 2 2 2 2 2 -1

The top-left will be array[0][0]. The bottom-right will be array[13][14].

I ran another function just to be sure the array was being populated in this fashion. It is. However, it is after the "initializeArray" function that nothing seems to happen, and the results being returned are 0's for the output in the "print" function.

Are you passing flower, water and path by reference in the execute function.
This should work:

void execute(int array[ROWS][COLS], int& flower, int& water, int& path)
{
for (int i = 0; i < 20; i++)
{
initializeArray(array);
int row = 6, col = 1; // 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);
}
}
}

You may also wish to seed the random number generator using srand.

Thank you. Passing by reference and changing the reset position fixed the code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.