Program runs fine doesn't want to compute

A new program I've been working on for a week now and wanted to get some advice on what should be altered for it to run properly. This isn't all my code, I've had assistance from classmates.

If you copy and run, you'll notice it'll print out the grid perfectly fine, I just can't get it to compute with just 2 runs, my ultimate goal is 7000 runs

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>

using namespace std;

//global constant variables
const int ROWS = 13;
const int COLS = 14;

//prototypes
void populateAry(int[][COLS]);
void printAry(int, ofstream&);
void runSim(int, int, int, int);
int walk(int[][COLS], int&, int&, int);
void update(int[][COLS], int&, int&, int, int&, int&, int&);
double calcCost(int);
void printResults(int, int, int, double, ofstream&);

//functions
void populateAry(int ary[ROWS][COLS])
{
    for(int r = 0; r<ROWS; r++)
        {
         for(int c = 0; c<COLS; c++)
         {
            //add -1's to array (water)
            if((r==0 || r==12) || ((c==0 || c==13) && r !=6))
            {
                ary[r][c]= -1;
            }
            //add 0's (safe path)
                else if (r==6 && c!=0 && c!=13)
                {
                    ary[r][c]= 0;
                }
                //add 3's (bridge)
                else if (r==6 && (c==0 || c==13))
                {
                     ary[r][c] = 3;
                }
                //add 2's (flowers)
                else{
                     ary[r][c]=2;
                }
            }
        }
    }

void printAry(int ary[ROWS][COLS], ofstream& out)
{

cout<<endl<<setw(40)<<"Flowers Unlimited Park\n\n";
out<<endl<<setw(40)<<"Flowers Unlimited Park\n\n";

    for(int r = 0; r<ROWS; r++)
        {
        for(int c = 0; c<COLS; c++)
        {
                cout<<"  "<<ary[r][c];
                out<<"  "<<ary[r][c];

                if(ary[r][c] >=0)
                {
                    cout<<" "; //extra space because of -
                    out<<" ";
                }
                if(c==13)
                {
                    cout<<endl;//new line for each row
                    out<<endl;
                }
            }
        }
    }
void runSim(int ary[ROWS][COLS], int flower, int water, int safe)
{

    for(int i = 0; i < 2 ; i++)
        {
            //reset field of flowers for each attempt
            populateAry(ary);
            int row = 6, col = 1;  //reset position
            int position = ary[row][col]; //start Harvey out with his first step, forward (to my right), for each attempt

           //this loop represents each attempt to walk across the island.
            while(position != 3 && position != -1)
                {
                position = walk(ary, row, col, position);

                //check position & update status
                update(ary, row, col, position, flower, water, safe);
            }
        }//end for loop
    }
    int walk(int ary[ROWS][COLS], int& row, int& col, int pos)
    {
        //move it, Harvey!!
        int step = rand() % 100 +1;

        //decide which way he stepped                               //A friend had this portion written, can you explain
        if(step <=45){ //forward 45%...which means to MY right.     //why the percentages are the way they are?
            pos = ary[row][++col];//move him one column to my right
        }
        else if (step<=70){ //left 25%...UPward
            pos = ary[--row][col]; //move him one row up
        }
        else if (step<=90){ //right 20%...DOWNward
            pos = ary[++row][col];
        }
        else if (step<=100){ //backward 10% ...to MY left
            pos = ary[row][--col]; //move him one column left
        }

        return pos;
    }

    void update(int ary[ROWS][COLS], int& row, int& col, int pos, int& flower, int& water, int& safe)
    {
        //did he step on a flower?
        if(pos ==2 || pos ==1)
            {
            //subtract one from that element in the array
            ary[row][col]-=1;
            //add to flower counter
            flower++;
        }

        if(pos == -1)
            { //he's in the water
            water++;
        }
        if(pos == 3)
        { //he's safe
            safe++;
        }
    }
    double calcCost(int flower)
    {
        return double((flower*5.0));
    }
    void printResults(int water, int safe, int flower, double avg, ofstream& out)
    {

        cout<<"\n Total number of times Harvey had to be rescued from the water: "<<water
        <<".\n\n Total number of times Harvey made it to a bridge: "<<safe
        <<".\n\n Total number of flowers Harvey destroyed: "<<flower
        <<fixed<<showpoint<<setprecision(2)<<".\n\n Average cost of Harvey's walk: $"<<avg<<endl<<endl;

        out<<"\n Total number of times Harvey had to be rescued from the water: "<<water
        <<".\n\n Total number of times Harvey made it to a bridge: "<<safe
        <<".\n\n Total number of flowers Harvey destroyed: "<<flower
        <<fixed<<showpoint<<setprecision(2)<<".\n\n Average cost of Harvey's walk: $"<<avg<<endl<<endl;
    }
    int main()
    {
        ofstream out;
        out.open("HarveyOUT.txt");

        //counters
        int water = 0, safe = 0, flower = 0;

        int ary[ROWS][COLS];

        populateAry(ary);
        printAry(ary, out); //just to make sure the array has been populated correctly

        //attempt to walk across the island
        runSim(ary, flower, water, safe);
        double avg = calcCost(flower);
        printResults(water, safe, flower, avg, out);

        out.close();
        return 0;
    }

    //Random Walk
    //The local town drunk, Harvey, has gotten himself into a mess.  Besides
    //being in his normal state, a state that earned him his title, he has made
    //his way into the Flowers Unlimited Park, a park where many flowers are
    //grown and display in their natural beauty.  The local ordinance fines
    //individuals $4.00 for each flower they pick or destroy.  This is enforce
    //to insure the beauty and the integrity of the park.  Now old Harv is not
    //only in the wrong state, drunk, and in the wrong place, the park, he is
    //also on a small island within the park, thirteen feet wide by 12 feet long.
    //The island is connected to the main land by two bridges one at each end
    //of the island.  Now Harv needs to get off the island and in the process,
    //it will cost him (ie stepping on flowers to get off the island).
    //There is a clear path down the middle of the island that leads from one
    //bridge to another.  The remainder of the island has flowers.  On the island,
    //there are two flowers growing per square foot.  What we need to find out
    //is how much is it going to cost Old Harv to walk from where he is (the end
    //of the bridge on the island (ie first step forward put  Harv on the island)
    //to either bridge and get off the island?
    //After studying Harv for many years, it is known that he doesn't walk a
    //straight line when he is in his preferred state.  His walking patterns are
    //as follows.  He steps forward 44% of the time, he steps left 27% of the
    //time, right 21% and back wards 8% of the time.  If Harvey steps onto
    //either bridge, we will consider that he has made it across the island.
    //If he steps into the water, he has finished walking and must be rescued.
    //If he steps off the path onto a square containing two flowers, he destroys
    //one.  If he steps into the same square later, he destroys the second flower.
    //Write a program that will compute the cost of Harvey's walk in the park.
    //
    //Inputs:	None
    //Outputs:	The cost (average) of Harvey's walk.
    //	The number of times Harvey made it to a bridge.
    //	The number of times Harvey had to be rescued from the water.
    //Restrictions:	Use a 2-d array.  Run the simulation 7000 times to generate the results.
    //Output:	Format output in a readable style.  (Columns maybe)
Last edited on
OUTPUT:
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

                Flowers Unlimited Park

  -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  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1

 Total number of times Harvey had to be rescued from the water: 0.

 Total number of times Harvey made it to a bridge: 0.

 Total number of flowers Harvey destroyed: 0.

 Average cost of Harvey's walk: $0.00
 
@CodeNovice01

I changed the variable flower, to a double. Passed all variables by reference. Added in srand(); and included ctime, to use srand();

I increased runsim to 7000. All works now.

I put explanations of walk function reasons, in that function.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include <ctime>

using namespace std;

//global constant variables
const int ROWS = 13;
const int COLS = 14;

//prototypes
void populateAry(int[][COLS]);
void printAry(int, ofstream&);
void runSim(int, int &, int &, int &);
int walk(int[][COLS], int &, int &, int &);
void update(int[][COLS], int &, int &, int &, double &, int &, int &);
double calcCost(double &);
void printResults(int &, int &, int &, double &, ofstream&);

//functions
void populateAry(int ary[ROWS][COLS])
{
	for(int r = 0; r<ROWS; r++)
	{
		for(int c = 0; c<COLS; c++)
		{
			//add -1's to array (water)
			if((r==0 || r==12) || ((c==0 || c==13) && r !=6))
			{
				ary[r][c]= -1;
			}
			//add 0's (safe path)
			else if (r==6 && c!=0 && c!=13)
			{
				ary[r][c]= 0;
			}
			//add 3's (bridge)
			else if (r==6 && (c==0 || c==13))
			{
				ary[r][c] = 3;
			}
			//add 2's (flowers)
			else{
				ary[r][c]=2;
			}
		}
	}
}

void printAry(int ary[ROWS][COLS], ofstream& out)
{

	cout<<endl<<setw(40)<<"Flowers Unlimited Park\n\n";
	out<<endl<<setw(40)<<"Flowers Unlimited Park\n\n";

	for(int r = 0; r<ROWS; r++)
	{
		for(int c = 0; c<COLS; c++)
		{
			cout<<"  "<<ary[r][c];
			out<<"  "<<ary[r][c];

			if(ary[r][c] >=0)
			{
				cout<<" "; //extra space because of -
				out<<" ";
			}
			if(c==13)
			{
				cout<<endl;//new line for each row
				out<<endl;
			}
		}
	}
}
void runSim(int ary[ROWS][COLS], double &flower, int &water, int &safe)
{

	for(int i = 0; i < 7000 ; i++)
	{
		//reset field of flowers for each attempt
		populateAry(ary);
		int row = 6, col = 1;  //reset position
		int position = ary[row][col]; //start Harvey out with his first step, forward (to my right), for each attempt

		//this loop represents each attempt to walk across the island.
		while(position != 3 && position != -1)
		{
			position = walk(ary, row, col, position);

			//check position & update status
			update(ary, row, col, position, flower, water, safe);
		}
	}//end for loop
}

int walk(int ary[ROWS][COLS], int& row, int& col, int &pos)
{
	//move it, Harvey!!
	int step = rand() % 100 +1;

	//decide which way he stepped                               //A friend had this portion written, can you explain
	if(step <=45){ // 1% to 45%
		pos = ary[row][++col];//move him one column to my right
	}
	else if (step<=70){ //45% to 70% (or a 25% chance  45+25 = 70)
	pos = ary[--row][col]; //move him one row up
	}
	else if (step<=90){ //70% to 90% ( or a 20% chance 70+20 = 90)
		pos = ary[++row][col];
	}
	else if (step<=100){ //10% chance only (90+10 = 100)
                         // With if/else, as soon one part is true, that gets run, and the rest, ignored
		pos = ary[row][--col]; //move him one column left
	}

	return pos;
}

void update(int ary[ROWS][COLS], int &row, int &col, int &pos, double &flower, int &water, int &safe)
{
	//did he step on a flower?
	if(pos ==2 || pos ==1)
	{
		//subtract one from that element in the array
		ary[row][col]-=1;
		//add to flower counter
		flower+=1.0;
	}

	if(pos == -1)
	{ //he's in the water
		water++;
	}
	if(pos == 3)
	{ //he's safe
		safe++;
	}
}

double calcCost(double &flower)
{
	return flower*5;
}

void printResults(int &water, int &safe, double &flower, double &avg, ofstream& out)
{

	cout<<"\n Total number of times Harvey had to be rescued from the water: "<<water
		<<".\n\n Total number of times Harvey made it to a bridge: "<<safe
		<<".\n\n Total number of flowers Harvey destroyed: "<<flower
		<<fixed<<showpoint<<setprecision(2)<<".\n\n Average cost of Harvey's walk: $"<<avg<<endl<<endl;

	out<<"\n Total number of times Harvey had to be rescued from the water: "<<water
		<<".\n\n Total number of times Harvey made it to a bridge: "<<safe
		<<".\n\n Total number of flowers Harvey destroyed: "<<flower
		<<fixed<<showpoint<<setprecision(2)<<".\n\n Average cost of Harvey's walk: $"<<avg<<endl<<endl;
}

int main()
{
	srand((unsigned)time(0));
	ofstream out;
	out.open("HarveyOUT.txt");

	//counters
	int water = 0, safe = 0;
	double flower = 0.0;
	int ary[ROWS][COLS];

	populateAry(ary);
	printAry(ary, out); //just to make sure the array has been populated correctly

	//attempt to walk across the island
	runSim(ary, flower, water, safe);
	double avg = calcCost(flower);
	printResults(water, safe, flower, avg, out);

	out.close();
	return 0;
}

//Random Walk
//The local town drunk, Harvey, has gotten himself into a mess.  Besides
//being in his normal state, a state that earned him his title, he has made
//his way into the Flowers Unlimited Park, a park where many flowers are
//grown and display in their natural beauty.  The local ordinance fines
//individuals $5.00 for each flower they pick or destroy.  This is enforce
//to insure the beauty and the integrity of the park.  Now old Harv is not
//only in the wrong state, drunk, and in the wrong place, the park, he is
//also on a small island within the park, eleven feet wide by 12 feet long.
//The island is connected to the main land by two bridges one at each end
//of the island.  Now Harv needs to get off the island and in the process,
//it will cost him (ie stepping on flowers to get off the island).
//There is a clear path down the middle of the island that leads from one
//bridge to another.  The remainder of the island has flowers.  On the island,
//there are two flowers growing per square foot.  What we need to find out
//is how much is it going to cost Old Harv to walk from where he is (the end
//of the bridge on the island (ie first step forward put  Harv on the island)
//to either bridge and get off the island?
//After studying Harv for many years, it is known that he doesn't walk a
//straight line when he is in his preferred state.  His walking patterns are
//as follows.  He steps forward 45% of the time, he steps left 25% of the
//time, right 20% and back wards 10% of the time.  If Harvey steps onto
//either bridge, we will consider that he has made it across the island.
//If he steps into the water, he has finished walking and must be rescued.
//If he steps off the path onto a square containing two flowers, he destroys
//one.  If he steps into the same square later, he destroys the second flower.
//Write a program that will compute the cost of Harvey's walk in the park.
//
//Inputs:	None
//Outputs:	The cost (average) of Harvey's walk.
//	The number of times Harvey made it to a bridge.
//	The number of times Harvey had to be rescued from the water.
//Restrictions:	Use a 2-d array.  Run the simulation 7000 times to generate the results.
//Output:	Format output in a readable style.  (Columns maybe) 
Im playing around with it. What i have found so far is that water flowers safe bridge ect were not being written to. hence all zeros. I made them global as a temp fix and that is giving me something.

The other thing i just noticed is the random function isnt very random... its spitting out the same series of numbers for whatever reason. Apparently theres more to using that function than just a simple call. I'd assume that this isn't as entertaining if Harvey just follows the same path everytime.

edit I was able to fix the randomness issue.

edit edit. Seems to be working ok. You may want to continue messing with it.

Also Note I made it change the value of ary[r][c] to 9 if harvey steped there so you can actually see the path he traveled. this may change the amount of flowers he steps on. I personally like the fact that i can see where he goes. For whatever reason if he steps in the water it doesn't change to 9.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include<iostream>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include <ctime>
using namespace std;

//global constant variables
const int ROWS = 13;
const int COLS = 14;

//prototypes
void populateAry(int[][COLS]);
void printAry(int, ofstream&);
void runSim(int, int, int, int);
int walk(int[][COLS], int&, int&, int);
void update(int[][COLS], int&, int&, int, int&, int&, int&);
double calcCost(int);
void printResults(int, int, int, double, ofstream&);

//functions
void populateAry(int ary[ROWS][COLS])
{
    for (int r = 0; r < ROWS; r++)
    {
        for (int c = 0; c < COLS; c++)
        {
            //add -1's to array (water)
            if ((r == 0 || r == 12) || ((c == 0 || c == 13) && r != 6))
            {
                ary[r][c] = -1;
            }
            //add 0's (safe path)
            else if (r == 6 && c != 0 && c != 13)
            {
                ary[r][c] = 0;
            }
            //add 3's (bridge)
            else if (r == 6 && (c == 0 || c == 13))
            {
                ary[r][c] = 3;
            }
            //add 2's (flowers)
            else {
                ary[r][c] = 2;
            }
        }
    }
}

void printAry(int ary[ROWS][COLS], ofstream& out)
{

    cout << endl << setw(40) << "Flowers Unlimited Park\n\n";
    out << endl << setw(40) << "Flowers Unlimited Park\n\n";

    for (int r = 0; r < ROWS; r++)
    {
        for (int c = 0; c < COLS; c++)
        {
            cout << "  " << ary[r][c];
            out << "  " << ary[r][c];

            if (ary[r][c] >= 0)
            {
                cout << " "; //extra space because of -
                out << " ";
            }
            if (c == 13)
            {
                cout << endl;//new line for each row
                out << endl;
            }
        }
    }
}
void runSim(int ary[ROWS][COLS], int& flower, int& water, int& safe)
{

    for (int i = 0; i < 2; i++)
    {
        //reset field of flowers for each attempt
        populateAry(ary);
        int row = 6, col = 1;  //reset position
        int position = ary[row][col]; //start Harvey out with his first step, forward (to my right), for each attempt
        ary[row][col] = 9;  //<<<<<~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~adds a 9 
       //this loop represents each attempt to walk across the island.
        while (position != 3 && position != -1)             //3 is bridge -1 is water... so success or fail
        {
            position = walk(ary, row, col, position);

            //check position & update status
            update(ary, row, col, position, flower, water, safe);
        }
    }//end for loop
}


int walk(int ary[ROWS][COLS], int& row, int& col, int pos)
{
    int drink = 0;
    //move it, Harvey!!
    int step = rand() % 100 + 1;     //0 to 32000ish / 100 remainder +1   so its never zero id assume
    
    //decide which way he stepped                               //A friend had this portion written, can you explain
    if (step <= 45 && col != COLS) { //forward 45%...which means to MY right.     //why the percentages are the way they are?
        pos = ary[row][++col];//move him one column to my right
    }
    else if (step <= 70 && row!=0) { //left 25%...UPward
        pos = ary[--row][col]; //move him one row up
    }
    else if (step <= 90 && row !=COLS) { //right 20%...DOWNward
        pos = ary[++row][col];
    }
    else if (step <= 100 && col != 0) { //backward 10% ...to MY left
        pos = ary[row][--col]; //move him one column left
    }
    else {
        drink++;
    }

    

    //cout << "walk() pos  " << pos << "  row  " << row << "  col   " << col << "  ary[row][col]  "<< ary[row][col]<< "  drink  "<< drink <<endl;

    //cout << endl;

    return pos;
}

void update(int ary[ROWS][COLS], int& row, int& col, int pos, int& flower, int& water, int& safe)
{
    //did he step on a flower?
    if (pos == 2 || pos == 1)
    {
        //subtract one from that element in the array
        ary[row][col] -= 1;
        //add to flower counter
        flower++;
    }

    if (pos == -1)
    { //he's in the water
       // cout << "water row col  " << row << "  " << col << endl;
        water++;
    }
    if (pos == 3)
    { //he's safe
        safe++;
    }
    ary[row][col] = 9; //<<<<<~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~adds a 9

    //cout << "update() pos  " << pos << "  row  " <<row << "  col  " << col << "  flower  "<<flower<<"  water  "<<water<<"  safe  "<< safe<<endl;
    //cout << endl;

}
double calcCost(int flower)
{
    return double((flower * 5.0));
}
void printResults(int water, int safe, int flower, double avg, ofstream& out)
{

    cout << "\n Total number of times Harvey had to be rescued from the water: " << water
        << ".\n\n Total number of times Harvey made it to a bridge: " << safe
        << ".\n\n Total number of flowers Harvey destroyed: " << flower
        << fixed << showpoint << setprecision(2) << ".\n\n Average cost of Harvey's walk: $" << avg << endl << endl;

    out << "\n Total number of times Harvey had to be rescued from the water: " << water
        << ".\n\n Total number of times Harvey made it to a bridge: " << safe
        << ".\n\n Total number of flowers Harvey destroyed: " << flower
        << fixed << showpoint << setprecision(2) << ".\n\n Average cost of Harvey's walk: $" << avg << endl << endl;
}




int main()
{
    srand(time(0));

//    const int ROWS = 13;
//const int COLS = 14;

    ofstream out;
    out.open("HarveyOUT.txt");

    //counters
    int water = 0, safe = 0, flower = 0;
int ary[ROWS][COLS];

    

    populateAry(ary);
     //just to make sure the array has been populated correctly

    //attempt to walk across the island
    runSim(ary, (int&)flower, (int&)water, (int&)safe);
    double avg = calcCost(flower);
printAry(ary, out);
    printResults(water, safe, flower, avg, out);

    out.close();
    return 0;
}


output

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

                Flowers Unlimited Park

  -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   9   9
  -1  2   2   2   2   2   2   2   2   2   9   9   9   -1
  -1  2   2   2   2   2   2   9   9   9   9   2   2   -1
  -1  9   9   9   9   9   9   9   2   9   9   2   2   -1
  3   9   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  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1  -1

 Total number of times Harvey had to be rescued from the water: 2.

 Total number of times Harvey made it to a bridge: 0.

 Total number of flowers Harvey destroyed: 45.

 Average cost of Harvey's walk: $225.00 
Last edited on
I also added in checks so that the walking function doesn't push the array out of bounds. If the array would go out of bounds then he doesn't move and takes a drink. You could even add in a drinking element where like every couple of flowers he drinks a beer and as he gets drunkier the %s of walking backwards or side to side increase.
This isn't all my code, I've had assistance from classmates.

LOL, even some from the Class of 2011.

https://www.daniweb.com/programming/software-development/threads/270686/2-d-array-not-being-passed-correctly
@whitenite1 && @MarkyRocks, thanks for your edits. I like how you altered the code to display his path he takes, now for each element that has a 2, i'd like it to display a 1 IF he walks into that element and then a 0 if he walks into twice(does that make sense?). How would I incorporate that into it?

Also, with the random function, professor doesn't want us to use srand, didn't give a reason, but i've noticed the runs are the same with just rand. i have the 100+1, because we're to use a number between 1 and 100. Are there any other alternatives to using rand that would produce different numbers each simulation?

I've gotten the code to compile and display correctly.

Since the run size has to be 7k, do you think the path would be worth displaying as output?


EDIT: I've gotten the program to display a 1 or 0 in any element where "Harvey" steps into. Also a 0 for when he falls into the water.

Last edited on
@CodeNovice01

Since the run size has to be 7k, do you think the path would be worth displaying as output?


In my opinion, no. If your professor hasn't requested it, it's not something he's interested in seeing. As for not using srand(), I believe that's just his/her preference.

Glad to see you were able to work out making the code changes for the array values. Feels good, doesn't it?
Yes, it feels great. It's always hard in the beginning, but as soon as I can see it coming together and what syntax to use it all falls into place.. Still have a long way with 2d or multi array before I master it. but all in time.

Thanks again for the input!
Topic archived. No new replies allowed.