Question

A robot is standing on an integer number line spanning the range from −∞ to ∞....

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 doesn’t 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 robot’s 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;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

I am also providing the screenshot of the program and the output for both the case for your confirmation.

You can positive number have taken 2n-1 movement while negative number takes 2n steps.

I also provide my code in C++:

#include<iostream>
#include<cmath>
using namespace std;
int main(){
int position,start=0;
int movement=0,count=1;
int direction=1;
cout<<"We always start from the 0.\n\n";
cout<<"Enter the position : ";
cin>>position;
while(start!=position){
if(direction==1){
start=start+count; //Go RIGHT
count++;
}
else{
start=start-count; //Go LEFT
count++;
}
movement=movement+1;
direction=-direction;
}
cout<<"No. of movements we perform : "<<movement;
}

-----xxxxx------

PLEASE GIVE YOUR VALUABLE FEEDBACK AND ALSO RATE MY EFFORTS.

THANK YOU

Add a comment
Know the answer?
Add Answer to:
A robot is standing on an integer number line spanning the range from −∞ to ∞....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT