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);
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;
}
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..
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.