-1

I've an array of 2500 steps taken by a robot, each step taken in a random direction (up, down, right, or left). I'm supposed to store the Euclidian distance (a right triangle's hypotenuse) of each step from the robot's origin. No problem there.

I'm also supposed to keep tabs on the max. Euclidian distance the robot reaches, so if the current distance is greater than one of the previous ones, the current one becomes the new greatest distance. Here's my code:

int main(){

int steps[2500];
int randDir[2500];
int coords[2] = {0,0};
int path[2500][2];
double eucliDist[2500];
eucliDist[0] = 1;
double maxEucliDist;
double taxiDist;

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

    randDir[i] = rand() % 4;
    steps[i] = i + 1;

    switch(randDir[i]){         
        case 0:
        coords[0] = coords[0] + 1;
        break;

        case 1:
        coords[1] = coords[1] + 1;
        break;

        case 2:
        coords[0] = coords[0] - 1;
        break;

        case 3:
        coords[1] = coords[1] - 1;
        break;
    }

    eucliDist[i] = sqrt((pow(coords[0],2)) + (pow(coords[1],2)));

    if (eucliDist[i] > eucliDist[i-1]){

        maxEucliDist = eucliDist[i]; //need to fix this. Not showing true max Euclid. Dist.

        taxiDist = abs(coords[0]) + abs(coords[1]);
    }       

    //cout << "Step " << steps[i] << " Euclidian distance from origin is: " << eucliDist[i] <<endl; //check euclidian dist each step

    //cout << steps[i] << "\t Coords (" << coords[0] << ", " << coords[1] << ")" << "\n"; //check coords with each step

}

cout << "Final Coordinates: (" << coords[0] << ", " << coords[1] << ")" << endl;
cout << "Maximum Euclidian distance was: " << maxEucliDist << endl;
cout << "'Taxicab' distance is: " << taxiDist << endl;
cin.get();}

Problem is that my output shows the wrong max, as shown in the snippet of my output below:

Program output showing incorrect maximum Euclidian distance

FYI, "taxicab" distance is the distance a 2nd robot would have to take to get to the 1st robot's position at max distance if it needed to (it's a right triangle's base + height since traveling on a grid).

Not sure what I'm doing wrong. Might have something to do with my if-statement in the bottom half of the code.

Any thoughts?

2 Answers2

0

Your problem is indeed your if-statement:

if (eucliDist[i] > eucliDist[i-1]){  // THIS IS WRONG
    maxEucliDist = eucliDist[i]; // THIS IS ACTUALLY OK
    taxiDist = abs(coords[0]) + abs(coords[1]);
}       

You're comparing your current distance to the distance in the PREVIOUS frame, not your maximum. You also need to initialize your maximum to zero, since it needs something to start with too, or else your comparison will be "current" versus "garbage". C++ does not initialize local variables to zero.

Your new if-statement should be this:

if (eucliDist[i] > maxEucliDist){
    maxEucliDist = eucliDist[i];
    taxiDist = abs(coords[0]) + abs(coords[1]);
}       
Kevin Anderson
  • 6,850
  • 4
  • 32
  • 54
  • Thanks, Kevin! I can't believe I didn't see that. I've been burning the candle at both ends for a few days now, and I swear I was seeing code everywhere I looked. Just took a second set of eyes to show me the obvious! – Marshall Prince Dec 17 '18 at 20:43
  • That's wierd. I just made the change, saved, recompiled, and ran it, only to get the exact same result. I opened a new terminal shell and did it again, but I'm still getting the exact same wrong answer. – Marshall Prince Dec 17 '18 at 20:52
0

Your first job is to recognise that the square of a positive number is a monotonic function of the number. So stop taking those square roots all over the place (which are difficult for a computer to evaluate), and work in distance squared, until you come to display the final result.

Then you can replace the nasty pow(x, 2) functions with x * x and work in integer arithmetic. (Take steps to avoid overflowing an int though.). This will be at least an order of magnitude faster.

Your specific issue is a logic error in that you are only comparing your new distance with the previous one, not the minimum found thus far.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Your first point depends whether the spec actually requires that the distances are stored, or only that they are found/calculated. Letter of the law specification. – Gem Taylor Dec 17 '18 at 20:43
  • @GemTaylor: Bend the spec! Provide a function to get the actual difference if necessary, perhaps even caching. If that proves impractical, then I guess the `pow` replacement can survive... – Bathsheba Dec 17 '18 at 20:44
  • Bathsheba, thanks for all of that info! I already love StackOverflow. So much more instructive than anything I've used before! – Marshall Prince Dec 17 '18 at 20:47