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?