Question: My assignment is running a simulation in my CS 2 class. I am required to move a person one step at a time through a

My assignment is running a simulation in my CS 2 class. I am required to move a person one step at a time through a 2-D array using random numbers to dictate direction. I have initialized the start and how he should move based on the random variable using if..else if statements. A random number between 0-44 means forward, 45-71 means to the right, 72-91 means left, 92-100 means backward. I nested all of it in a do..while statement. Unfortunately, I had it cout the randomly generated number and go through the steps to determine it is functioning correctly. It is infinitely looping as after one step it prints no other integers.

#include

using namespace std;

int RandomStep() { return rand() % 100 + 1; }

int main() { int step[15][14] = { {-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}, {-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}, {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1} }; int stumble,rescued,MadeIt,row,col; float total;

total = 0; rescued = 0; MadeIt = 0; step[row][col] = step[7][0]; do { stumble = RandomStep(); cout << stumble << endl;

if (stumble < 45) { step[row][col] = step[row][col + 1]; if (step[row][col] == 0) { step[row][col] = 0; total = total + 0; } else if (step[row][col] == 2) { step[row][col] = 1; total = total + 4; } else if (step[row][col] == 1) { step[row][col] = 0; total = total + 4; } } else if (stumble < 72 || stumble > 44) { step[row][col] = step[row + 1][col]; if (step[row][col] == 0) { step[row][col] = 0; total = total + 0; } else if (step[row][col] == 2) { step[row][col] = 1; total = total + 4; } else if (step[row][col] == 1) { step[row][col] = 0; total = total + 4; } } else if (stumble < 93 || stumble > 71) { step[row][col] = step[row - 1][col]; if (step[row][col] == 0) { step[row][col] = 0; total = total + 0; } else if (step[row][col] == 2) { step[row][col] = 1; total = total + 4; } else if (step[row][col] == 1) { step[row][col] = 0; total = total + 4; } } else if (stumble > 92) { step[row][col] = step[row][col - 1]; if (step[row][col] == 0) { step[row][col] = 0; total = total + 0; } else if (step[row][col] == 2) { step[row][col] = 1; total = total + 4; } else if (step[row][col] == 1) { step[row][col] = 0; total = total + 4; } } } while (step[row][col] != -1 || step[row][col] != 3);

return 0; }

Why is it not completing one iteration and starting the random number process again? Some of the variables do not have purposes yet. Juts trying to fix the body first.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!