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.

 
(-1,0)
(-2,0)
(-1,0)
(-2,0)
(-2,1)
(-2,2)
(-2,1)
(-2,0)
(-3,0)
(-2,0)
(-2,-1)
(-1,-1)
(-1,-2)
(-2,-2)
(-2,-3)
(-2,-4)
(-2,-5)
Hit the boundary!
Left
Left
Right
Left
Right
Up
Right
(1,0)
(1,1)
(2,1)
(2,0)
(2,1)
(2,2)
(2,1)
(1,1)
(0,1)
(-1,1)
(0,1)
(1,1)
(1,0)
(0,0)
Back to the origin!
Up
Up
Down
Down
Up
Down
Up
Left
Down
Right
Left
Down
Left
Right
Left
Down
Right
Right
Left
Down
Down
Down
Left
Down
or
Transcribed Image Text:(-1,0) (-2,0) (-1,0) (-2,0) (-2,1) (-2,2) (-2,1) (-2,0) (-3,0) (-2,0) (-2,-1) (-1,-1) (-1,-2) (-2,-2) (-2,-3) (-2,-4) (-2,-5) Hit the boundary! Left Left Right Left Right Up Right (1,0) (1,1) (2,1) (2,0) (2,1) (2,2) (2,1) (1,1) (0,1) (-1,1) (0,1) (1,1) (1,0) (0,0) Back to the origin! Up Up Down Down Up Down Up Left Down Right Left Down Left Right Left Down Right Right Left Down Down Down Left Down or
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