# please tell me how to rectify my code

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 12-20-2010
laserlight
Actually, I think the correct formula is sqrt(|x1 - x2|^2 + |y1 - y2|^2), i.e.,
Code:

```const double latitude_dist = p1.latitude - p2.latitude; const double longitude_dist = p1.longitude - p2.longitude; distance = sqrt(latitude_dist * latitude_dist + longitude_dist * longitude_dist);```
• 12-20-2010
GReaper
Oops... :D:D:D
• 12-20-2010
ilikeapplepie
Code:

```#include <stdio.h> #include <math.h> struct position{         double latitude;         double longitude; }; int  main(void) {                double distance;         struct position p1,p2;         scanf( "%f, %f, %f, %f", &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude);         const double latitude_dist = p1.latitude - p2.latitude;         const double longitude_dist = p1.longitude - p2.longitude;         distance = sqrt(latitude_dist * latitude_dist + longitude_dist * longitude_dist);         printf(" %f", distance); }```
yet no luck. rebuilt, debugged and ran.
can you please tell me if the above code works on your computer.
than you for your patience and help.
• 12-20-2010
GReaper
scanf+double ==> %lf :)
• 12-20-2010
ilikeapplepie
Quote:

Originally Posted by Sipher
scanf+double ==> %lf :)

sorry, still no help.
i am a newbie i don't know what's wrong with my compiler. however, if the code works on your compiler at least i know i am on the right track.
• 12-20-2010
laserlight
Try this:
Code:

```#include <stdio.h> #include <math.h> struct position {     double latitude;     double longitude; }; double computeDistance(const struct position *p1, const struct position *p2) {     const double latitude_dist = p1->latitude - p2->latitude;     const double longitude_dist = p1->longitude - p2->longitude;     return sqrt(latitude_dist * latitude_dist + longitude_dist * longitude_dist); } int main(void) {     struct position p1, p2;     if (scanf("%lf, %lf, %lf, %lf",         &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude) == 4)     {         printf("Distance = %f\n", computeDistance(&p1, &p2));     }     else     {         puts("Input error");     }     return 0; }```
• 12-20-2010
ilikeapplepie
input :
1 2 3 4

output : input error
• 12-20-2010
GReaper
Now how's that happening?! Maybe removing the commas in scanf?
• 12-20-2010
ilikeapplepie
Quote:

Originally Posted by Sipher
Now how's that happening?! Maybe removing the commas in scanf?

yes, that worked no error. ty vry much. but now i am still getting a big number even with someone else's code.
something must be wrong with my compiler. i am really grateful for your help.
• 12-20-2010
GReaper
Why don't you post the four values that you inputed and the result you got? Maybe you just think it's huge while it's ok.
• 12-20-2010
ilikeapplepie
Quote:

Originally Posted by Sipher
Why don't you post the four values that you inputed and the result you got? Maybe you just think it's huge while it's ok.

input
1 2 3 4

output
1726716016253713000000000000000000000000.000000000
• 12-20-2010
GReaper
o_O O_O O_o ...
• 12-20-2010
laserlight
Quote:

Originally Posted by ilikeapplepie
now i am still getting a big number even with someone else's code.

Quote:

Originally Posted by ilikeapplepie
something must be wrong with my compiler.

• 12-20-2010
ilikeapplepie
Quote:

Originally Posted by laserlight

i copied and pasted your code. i was getting input error till. i removed commas from scanf then i got the big number.

visual c++ 2008 edition.

Code:

```#include <stdio.h> #include <math.h> struct position {     double latitude;     double longitude; }; double computeDistance(const struct position *p1, const struct position *p2) {     const double latitude_dist = p1->latitude - p2->latitude;     const double longitude_dist = p1->longitude - p2->longitude;     return sqrt(latitude_dist * latitude_dist + longitude_dist * longitude_dist); } int main(void) {     struct position p1, p2;     if (scanf("%lf %lf %lf %lf",         &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude) == 4)     {         printf("Distance = %f\n", computeDistance(&p1, &p2));     }     else     {         puts("Input error");     }     return 0; }```
and this is my own code. i was getting all errors till you helped but here too, big number.
Code:

```#include <stdio.h> #include <math.h> struct position{         double latitude;         double longitude; }; int  main(void) {                double distance_lat;         double distance_lon;         double distance;         struct position p1,p2;         scanf( "%f, %f, %f, %f", &p1.latitude, &p1.longitude, &p2.latitude, &p2.longitude);         distance_lat=p1.latitude-p2.latitude;         distance_lon=p1.longitude-p2.longitude;         distance = sqrt( (distance_lat * distance_lat) + (distance_lon * distance_lon));         printf(" %f", distance); }```
• 12-20-2010
tabstop
And what are you using for input, and what are you getting for output?
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last