the closest city c program please help

closed account (DLUk92yv)
So I have this assignment that asks me to show the name of the closest city.
There 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).).

And here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <math.h>

int main()
{
    char* name1[3][16];
    float Lat1, Long1;
    Long1>=-180 && Long1<=180, Lat1>=-90 && Lat1<=90;
    scanf("%s %f %f", name1, &Lat1, &Long1);

    char* name2[3][16];
    float Lat2, Long2;
    Long2>=-180 && Long2<=180, Lat2>=-90 && Lat2<=90;
    scanf("%s %f %f", name2, &Lat2, &Long2);

    char* name3[3][16];
    float Lat3, Long3;
    Long3>=-180 && Long3<=180, Lat3>=-90 && Lat3<=90;
    scanf("%s %f %f", name3, &Lat3, &Long3);

    float dist1, dist2;
    dist1=sqrt((Lat2-Lat1)*(Lat2-Lat1)+(Long2-Long1)*(Long2-Long1));
    dist2=sqrt((Lat3-Lat2)*(Lat3-Lat2)+(Long3-Long2)*(Long3-Long2));


if(dist1<dist2)
printf("%s", name3);


return 0;

}





The problem here is that when i submit the question it says wrong answer and I dont know what part of the code is wrong
Last edited on
closed account (DLUk92yv)
oh and I know that this part "Long1>=-180 && Long1<=180, Lat1>=-90 && Lat1<=90; " has no function at all.
Lines 8,13,18: So if you know these lines have no function, why are they there?
Perhaps make them comments instead of useless code?

Lines 6,11,16: Why are your names 2D arrays?

Line 26-27: Why are you printing name3 if dist1 (city2) is closer than dist2? If dist1 is less than dist2, then city2 is the closer city. Don't you also want to print the name of city 3, if dist2 is closer?

BTW, the names dist1 and dist2 are confusing. dist1 is the distance to city2 and dist2 is the distance to city 3. Using confusing names makes your program hard to follow.
Also, an sphere is not a plane.
Topic archived. No new replies allowed.