A robot is initially located at position (0, 0) in a grid [−5, 5] × [−5, 5]

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

(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.

 
### Movement Sequences

#### Left Panel
- **Right** to (1,0)
- **Up** to (1,1)
- **Right** to (2,1)
- **Down** to (2,0)
- **Up** to (2,1)
- **Up** to (2,2)
- **Down** to (2,1)
- **Left** to (1,1)
- **Left** to (0,1)
- **Left** to (-1,1)
- **Right** to (0,1)
- **Right** to (1,1)
- **Down** to (1,0)
- **Left** to (0,0)
- **Message:** Back to the origin!

#### Right Panel
- **Left** to (-1,0)
- **Left** to (-2,0)
- **Right** to (-1,0)
- **Left** to (-2,0)
- **Up** to (-2,1)
- **Up** to (-2,2)
- **Down** to (-2,1)
- **Down** to (-2,0)
- **Left** to (-3,0)
- **Right** to (-2,0)
- **Down** to (-2,-1)
- **Right** to (-1,-1)
- **Down** to (-1,-2)
- **Left** to (-2,-2)
- **Down** to (-2,-3)
- **Down** to (-2,-4)
- **Down** to (-2,-5)
- **Message:** Hit the boundary!

### Diagram Explanation
The diagrams illustrate sequences of movements on a coordinate grid. Each move is annotated with a direction (Right, Left, Up, Down) and the resulting coordinate. The left sequence returns to the origin at (0,0), while the right sequence ends at a boundary, indicated by the message. These illustrate possible paths in a grid-based system or game environment.
Transcribed Image Text:### Movement Sequences #### Left Panel - **Right** to (1,0) - **Up** to (1,1) - **Right** to (2,1) - **Down** to (2,0) - **Up** to (2,1) - **Up** to (2,2) - **Down** to (2,1) - **Left** to (1,1) - **Left** to (0,1) - **Left** to (-1,1) - **Right** to (0,1) - **Right** to (1,1) - **Down** to (1,0) - **Left** to (0,0) - **Message:** Back to the origin! #### Right Panel - **Left** to (-1,0) - **Left** to (-2,0) - **Right** to (-1,0) - **Left** to (-2,0) - **Up** to (-2,1) - **Up** to (-2,2) - **Down** to (-2,1) - **Down** to (-2,0) - **Left** to (-3,0) - **Right** to (-2,0) - **Down** to (-2,-1) - **Right** to (-1,-1) - **Down** to (-1,-2) - **Left** to (-2,-2) - **Down** to (-2,-3) - **Down** to (-2,-4) - **Down** to (-2,-5) - **Message:** Hit the boundary! ### Diagram Explanation The diagrams illustrate sequences of movements on a coordinate grid. Each move is annotated with a direction (Right, Left, Up, Down) and the resulting coordinate. The left sequence returns to the origin at (0,0), while the right sequence ends at a boundary, indicated by the message. These illustrate possible paths in a grid-based system or game environment.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education