Hello! im new here and i have a problem regarding programming. We are required to compute for the distance of a certain city to another city...for example whats the distance of new york to los angeles..then the program will compute its distance... the output of the said program should be like this..

From: /name of city/
To: /name of city/
Total distance between cities: /value/

or

1st city:
2nd city:
distance between 2 cites:

it can be any method as much as possible a simple one that even a noob like me can understand..it could not necessarily be an array or struct as long as it is a c program...
i really need help in this..i would appreciate any help from you guys... you can message me, reply to this post. thank you very much!

Dani AI

Generated

As asked for a simple C solution: store each city�s latitude/longitude and compute the great‑circle distance between those coordinates. Contrary to �s post, C can do this easily — use the Haversine formula to get the surface distance (in km or miles). closed this as a duplicate, but a compact example below should make the idea clear for a beginner.

The core points:

  • You need each city�s latitude and longitude (degrees).
  • Convert degrees to radians before using trig functions.
  • Use a known Earth radius (e.g. 6371.0 km) for distance = R * central_angle.
  • Haversine is good for straight-line "as the crow flies" distances; driving distances require route data or an API.

A minimal example: choose two cities from a printed list and compute the Haversine distance.

#include <stdio.h>
#include <math.h>

#define R_EARTH_KM 6371.0
#define DEG2RAD (M_PI/180.0)

double haversine(double lat1, double lon1, double lat2, double lon2) {
    double dlat = (lat2 - lat1) * DEG2RAD;
    double dlon = (lon2 - lon1) * DEG2RAD;
    lat1 *= DEG2RAD; lat2 *= DEG2RAD;
    double a = sin(dlat/2)*sin(dlat/2) + cos(lat1)*cos(lat2)*sin(dlon/2)*sin(dlon/2);
    double c = 2 * atan2(sqrt(a), sqrt(1-a));
    return R_EARTH_KM * c;
}

int main(void) {
    struct city { const char *name; double lat, lon; };
    const struct city cities[] = {
        {"New York", 40.7128, -74.0060},
        {"Los Angeles", 34.0522, -118.2437},
        {"Chicago", 41.8781, -87.6298},
        {"London", 51.5074, -0.1278}
    };
    int n = sizeof cities / sizeof cities[0], a, b;
    printf("Cities:\\n");
    for (int i=0;i<n;i++) printf("%d: %s\\n", i+1, cities[i].name);
    printf("Enter number of first city: "); if (scanf("%d",&a)!=1) return 1;
    printf("Enter number of second city: "); if (scanf("%d",&b)!=1) return 1;
    if (a<1||a>n||b<1||b>n) { printf("Invalid selection\\n"); return 1; }
    double d = haversine(cities[a-1].lat, cities[a-1].lon, cities[b-1].lat, cities[b-1].lon);
    printf("From: %s\\nTo: %s\\nTotal distance between cities: %.1f km\\n", cities[a-1].name, cities[b-1].name, d);
    return 0;
}

Notes/troubleshooting:

  • Ensure coordinates use degrees (North positive, East positive; West/ South negative).
  • If you want name lookup instead of numbered choices, use case-insensitive string comparison or a small lookup table/file.
  • For higher geodetic accuracy over long distances, use Vincenty formulas or a geodesy library. See the Haversine formula for details: Haversine formula.

Recommended Answers

All 4 Replies

Quit spamming the board!

Hello! im new here and i have a problem regarding programming. We are required to compute for the distance of a certain city to another city...for example whats the distance of new york to los angeles..then the program will compute its distance... the output of the said program should be like this..

From: /name of city/
To: /name of city/
Total distance between cities: /value/

or

1st city:
2nd city:
distance between 2 cites:

it can be any method as much as possible a simple one that even a noob like me can understand..it could not necessarily be an array or struct as long as it is a c program...
i really need help in this..i would appreciate any help from you guys... you can message me, reply to this post. thank you very much!

i dont think there is any method to compute distance using c.
if there exists such method let me know..

Since this thread is similar to a thread in the C++ forum, and duplicate threads makes it difficult to keep track of the replies, I am closing this thread. Refer this link for replies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.