C programming about distance, please i need help

closed account (DLUk92yv)
i am asked to find Euclidean distance of some cities. I will be given Latitude and Longitude coordinates of a city and then, two other cities. Find the closer city to the first one.
A string (city name) and two floating point numbers (Latitude and Longitude coordinates of each city) in three lines where (W/E) -180 ≤ Longitude ≤ 180 and (S/N) -90 ≤ Latitude ≤ 90. And, city name is not longer than 15 characters and contains only English letters (no special characters).

So this is my code but I have a problem because I'm not really sure how should i calculate the distance..because when i run the program it asks me to put input for the distance but what i really want is for those to be calculated.Can you please help me out?


[code]
#include <stdio.h>
#include <math.h>

int main()
{


char name[15]; int Lat, Long;
Long>=-180 && Long<=180, Lat>=-90 && Lat<=90;
scanf("%s %d %d", name, &Lat, &Long);


char name1[15]; int Lat1, Long1;
Long1>=-180 && Long1<=180, Lat1>=-90 && Lat1<=90;
scanf("%s %d %d", name1, &Lat1, &Long1);



char name2[15]; int Lat2, Long2;
Long2>=-180 && Long2<=180, Lat2>=-90 && Lat2<=90;
scanf("%s %d %d", name2, &Lat2, &Long2);

int dist1=sqrt((Lat-Lat1)^2+(Long-Long1)^2);
int dist2=sqrt((Lat-Lat2)^2+(Long-Long2)^2);
if (dist1<dist2)
printf("%s", name2);
else if (dist1>dist2)
printf("%s", name1);
return 0;
}
closed account (DLUk92yv)
there's something wrong there since it is asked for the Lat and the Long to be float numbers and i know that but even with float it wont work
Hello ESB25,

I find expressing (Lat-Lat1)^2 as both intuitive and natural, but unfortunately as of 2014, it is not allowed as part of standard C. For the time being I`d suggest to use pow(Lat-Lat1, 2f) as work around..

All the best,
Dean
Check the return values of scan(). They may indicate an error and should provide the number of input items assigned on success. (see scanf(3)).

I dont' understand those three lines above your scanf() statements. They generate a boolean value but don't assign it nor do they use it for any conditional expression. As I think, an optimizing compiler would warn about useless code there and would generate no code for them.

Be careful with your cities names. 15 characters aren't enough if a cities name consists of exactly 15 character because scanf(3) appends a string terminating NUL-character. So use 15 + 1 character. Better use std::string.
sqrt() does return a value of type double, not int. So you'll lose the fractional part of sqrt() result.
closed account (DLUk92yv)
DEAN thank you :)

TCS: but what should i do about calculating dist1 and dist2??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <typename T>
T sqr(T val)
{
    return val * val;
} /* sqr() */

int main()
{
// ...

    double dist1 = sqrt(sqr(Lat-Lat1) + sqr(Long-Long1));

// ...
}
closed account (DLUk92yv)
thank youuu
Topic archived. No new replies allowed.