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