# Thread: please tell me how to rectify my code

1. 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);```

2. Oops...

3. 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.

4. scanf+double ==> %lf

5. 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.

6. 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;
}```

7. input :
1 2 3 4

output : input error

8. Now how's that happening?! Maybe removing the commas in scanf?

9. 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.

10. 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.

11. 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. o_O O_O O_o ...

13. Originally Posted by ilikeapplepie
now i am still getting a big number even with someone else's code.
What is your current code?

Originally Posted by ilikeapplepie
something must be wrong with my compiler.
What is your compiler?

14. Originally Posted by laserlight
What is your current code?

What is your compiler?
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);
}```

15. And what are you using for input, and what are you getting for output?

Popular pages Recent additions