(Random Walk Robot) A robot is initially located at position (0, 0) in a grid [−5, 5] × [−5, 5]. The robot can move randomly in any of the directions: up, down, left, right. The robot can only move one step at a time. For each move, print the direction of the move in and the current position of the robot. Use formatted output to print the direction (Down, Up, Left or Right) in the left. The direction takes 10 characters in total and fill in the field with empty spaces. The statement to print results in such format is given below: cout << setw(10) << left << ‘Down’ << ... ; cout << setw(10) << left << ‘Up’ << ...; If the robot moves back to the original place (0,0), print “Back to the origin!” to the console and stop the program. If it reaches the boundary of the grid, print “Hit the boundary!” to the console and stop the program. A successful run of your code may look like: Due to randomness, your results may have a different trajectory path than examples. Your code should generate different robot trajectories for each run. Hint: Use two int variables x and y to represent robot’s position and use the random integer generator to pick a moving direction at each step. Use a loop to keep moving the robot as long as the robot doesn’t hit the boundary or go back to the origin. *Without using "switch" statement
(Random Walk Robot) A robot is initially located at position (0, 0) in a grid [−5, 5] × [−5, 5]. The robot can move randomly in any of the directions: up, down, left, right. The robot can only move one step at a time.
For each move, print the direction of the move in and the current position of the robot. Use formatted output to print the direction (Down, Up, Left or Right) in the left. The direction takes 10 characters in total and fill in the field with empty spaces. The statement to print results in such format is given below:
cout << setw(10) << left << ‘Down’ << ... ; cout << setw(10) << left << ‘Up’ << ...;
If the robot moves back to the original place (0,0), print “Back to the origin!” to the console and stop the program. If it reaches the boundary of the grid, print “Hit the boundary!” to the console and stop the program. A successful run of your code may look like: Due to
randomness, your results may have a different trajectory path than examples. Your code should generate different robot trajectories for each run.
Hint: Use two int variables x and y to represent robot’s position and use the random integer generator to pick a moving direction at each step. Use a loop to keep moving the robot as long as the robot doesn’t hit the boundary or go back to the origin.
*Without using "switch" statement
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 4 images