Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A robot is standing on an integer number line spanning the range from to . Its position, x, starts at 0. There is an antenna

A robot is standing on an integer number line spanning the range from to . Its position, x, starts at 0. There is an antenna at an unknown location y that the robot must reach as quickly as possible in order to repair it. Since it doesnt know whether to move left or right, it searches in both directions by first moving one step to the right, then two steps to the left, then three to the right, and so on until it hits the antenna. So the locations it touches are as follows: (0, +1, 1, +2, 2, +3, 3, . . .). What is the -runtime of the robots search in terms of integer unit steps, if the antenna is n steps away? You should get the same answer regardless of whether its to the left or right.

Concretely, the robot plans its motion according to the following C++ code:

int position = 0;

int movement = 1;

int direction = 1;

while (!checkForPole(position)) {

for (int i = 0; i < movement; i++) {

if (direction == 1) goRight();

else goLeft(); }

movement += 1;

direction = -direction;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Database Programming With Visual Basic.NET

Authors: Ying Bai

1st Edition

0521712351, 978-0521712354

More Books

Students also viewed these Databases questions