(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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
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.

 

*Without using "switch" statement

Please enter a positive integer: 5
Square root of 5 is located in the interval [0, 5].
Square root of 5 is located in the interval [0, 2.5].
Square root of 5 is located in the interval [1.25, 2.5].
Square root of 5 is located in the interval [1.875, 2.5].
Square root of 5 is located in the interval [2.1875, 2.5].
Square root of 5 is located in the interval [2.1875, 2.34375].
Square root of 5 is located in the interval [2.1875, 2.265625].
Square root of 5 is located in the interval [2.2265625, 2.265625].
Square root of 5 is located in the interval [2.2265625, 2.24609375].
Square root of 5 is located in the interval [2.2265625, 2.236328125].
Square root of 5 is located in the interval [2.231445312, 2.236328125].
Square root of 5 is located in the interval [2.233886719, 2.236328125].
Square root of 5 is located in the interval [2.235107422,
Square root of 5 is located in the interval [2.235717773, 2.236328125].
Square root of 5 is located in the interval [2.236022949, 2.236328125].
Square root of 5 is located in the interval [2.236022949, 2.236175537].
Square root of 5 is located in the interval [2.236022949, 2.236099243].
Square root of 5 is located in the interval [2.236061096, 2.236099243].
Square root of 5 is located in the interval [2.236061096, 2.23608017].
Square root of 5 is located in the interval [2.236061096, 2.236070633].
The interval length is 9.536743164e-06.
236328125].
Please enter
positive integer: 4
Square root of 4 is located in the interval [0, 4].
Square root of 4 is located in the interval [2, 4].
Square root of 4 is located in the interval [2, 3].
Square root of 4 is located in the interval [2, 2.5].
Square root of 4 is located in the interval [2, 2.25].
Square root of 4 is located in the interval [2, 2.125].
Square root of 4 is located in the interval [2, 2.0625].
Square root of 4 is located in the interval [2, 2.03125].
Square root of 4 is located in the interval [2, 2.015625].
Square root of 4 is located in the interval [2, 2.0078125].
Square root of 4 is located in the interval [2, 2.00390625].
Square root of 4 is located in the interval [2, 2.001953125].
Square root of 4 is located in the interval [2, 2.000976562].
Square root of 4 is located in the interval [2, 2.000488281].
Square root of 4 is located in the interval [2, 2.000244141].
Square root of 4 is located in the interval [2, 2.00012207].
Square root of 4 is located in the interval [2, 2.000061035].
Square root of 4 is located in the interval [2, 2.000030518].
Square root of 4 is located in the interval [2, 2.000015259].
Square root of 4 is located in the interval [2, 2.000007629].
The interval length is 7.629394531e-06.
Transcribed Image Text:Please enter a positive integer: 5 Square root of 5 is located in the interval [0, 5]. Square root of 5 is located in the interval [0, 2.5]. Square root of 5 is located in the interval [1.25, 2.5]. Square root of 5 is located in the interval [1.875, 2.5]. Square root of 5 is located in the interval [2.1875, 2.5]. Square root of 5 is located in the interval [2.1875, 2.34375]. Square root of 5 is located in the interval [2.1875, 2.265625]. Square root of 5 is located in the interval [2.2265625, 2.265625]. Square root of 5 is located in the interval [2.2265625, 2.24609375]. Square root of 5 is located in the interval [2.2265625, 2.236328125]. Square root of 5 is located in the interval [2.231445312, 2.236328125]. Square root of 5 is located in the interval [2.233886719, 2.236328125]. Square root of 5 is located in the interval [2.235107422, Square root of 5 is located in the interval [2.235717773, 2.236328125]. Square root of 5 is located in the interval [2.236022949, 2.236328125]. Square root of 5 is located in the interval [2.236022949, 2.236175537]. Square root of 5 is located in the interval [2.236022949, 2.236099243]. Square root of 5 is located in the interval [2.236061096, 2.236099243]. Square root of 5 is located in the interval [2.236061096, 2.23608017]. Square root of 5 is located in the interval [2.236061096, 2.236070633]. The interval length is 9.536743164e-06. 236328125]. Please enter positive integer: 4 Square root of 4 is located in the interval [0, 4]. Square root of 4 is located in the interval [2, 4]. Square root of 4 is located in the interval [2, 3]. Square root of 4 is located in the interval [2, 2.5]. Square root of 4 is located in the interval [2, 2.25]. Square root of 4 is located in the interval [2, 2.125]. Square root of 4 is located in the interval [2, 2.0625]. Square root of 4 is located in the interval [2, 2.03125]. Square root of 4 is located in the interval [2, 2.015625]. Square root of 4 is located in the interval [2, 2.0078125]. Square root of 4 is located in the interval [2, 2.00390625]. Square root of 4 is located in the interval [2, 2.001953125]. Square root of 4 is located in the interval [2, 2.000976562]. Square root of 4 is located in the interval [2, 2.000488281]. Square root of 4 is located in the interval [2, 2.000244141]. Square root of 4 is located in the interval [2, 2.00012207]. Square root of 4 is located in the interval [2, 2.000061035]. Square root of 4 is located in the interval [2, 2.000030518]. Square root of 4 is located in the interval [2, 2.000015259]. Square root of 4 is located in the interval [2, 2.000007629]. The interval length is 7.629394531e-06.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY