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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started