mattaltepeter 0 Report post Posted September 5, 2010 (edited) I am working on a C program to calculate the shortest distance and the rectilinear distance between two points. However, when the program tries to calculate absolute value it doesn't do it correctly every time. any and all help is greatly appreciated #include<stdio.h>#include<stdlib.h>int main(){ int x1; //variable to hold the x-coordinate of the first point int x2; //variable to hold the x-coordinate of the second point int y1; //variable to hold the y-coordinate of the first point int y2; //variable to hold the y-coordinate of the second point float short_distance; //variable to hold the shortest distance int rectil; //variable to hold the rectilinear distance printf("Enter the first point: "); scanf("%d", &x1); scanf("%d", &y1); printf("\nEnter the second point: "); scanf("%d", &x2); scanf("%d", &y2); short_distance = sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1))); printf("\nShortest distance between points: %.2f\n",short_distance); rectil = abs(x1-y1) + abs(x2-y2); printf("Rectilinear distance between points: %d\n", rectil); return(0);} Edited September 5, 2010 by rvalkass (see edit history) Share this post Link to post Share on other sites
rvalkass 5 Report post Posted September 5, 2010 I believe your formula for the rectilinear distance is incorrect. Rather than just give you the answer, see if you can work out what is wrong. Come back with your debugging attempts if you can't work it out, and I'll give you some more advice. Debugging is a valuable skill, and one you won't learn with people giving you the answers. Harsh but what I believe to be important Share this post Link to post Share on other sites
mattaltepeter 0 Report post Posted September 5, 2010 okay so i messed around with it a little more. is it: |x1-x2|+|y1-y2|? Share this post Link to post Share on other sites
mattaltepeter 0 Report post Posted September 5, 2010 Okay my program is working correctly now. thanks for your advice Share this post Link to post Share on other sites
Quatrux 4 Report post Posted September 6, 2010 I suggest you to read into a* star algorithm, which is using your formula to calculate the path, it should go.. Your formula only gets the shortest distance, but what if there are walls?You can read about it here:http://forums.xisto.com/no_longer_exists/ used this tutorial, to create some pathfinding programs:http://www.policyalmanac.org/games/aStarTutorial.htm Share this post Link to post Share on other sites