Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*File contents: Travel planning using a structure
- Authors: Daniel Dedoukh, Mohamed Moustafa, Momme König*/
- #define _CRT_SECURE_NO_DEPRECATE
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define PI 3.14159265
- double distanceKm(const double *longitude, const double *attitude, double *totalDistance, int numWayPoints);
- int main(void)
- {
- //This structure will store longitude and latitude
- struct coordinate {
- double *latitude;
- double *longitude;
- } coordinate;
- int numWayPoints;
- double totalDistance = 0.0;
- printf("Enter number of waypoints: ");
- // To avoid user input mistakes.
- while ((!scanf("%d", &numWayPoints)) || (numWayPoints < 0))
- {
- char invalid;
- printf("\nInvalid input: ");
- while ((invalid = getchar()) != '\n')
- putchar(invalid);
- printf("\nTry again: \n");
- }
- //Using memory allocation to specify the number of points with respect to user input
- coordinate.latitude = (double *)calloc(numWayPoints, sizeof(double));
- coordinate.longitude = (double *)calloc(numWayPoints, sizeof(double));
- printf("\nEnter waypoints as \"<latitude> <longitude>\" separated by space");
- // ask the user
- for (int i = 0, count = 1; i < numWayPoints, count <= numWayPoints; i++, count++)
- {
- printf("\nWaypoint %d:", count);
- while (scanf("%lf %lf", &coordinate.latitude[i], &coordinate.longitude[i]) != 2)
- {
- char invalid;
- printf("\nInvalid input: ");
- while ((invalid = getchar()) != '\n')
- putchar(invalid);
- printf("\nTry again: \n");
- }
- getchar();
- }
- printf("By taking this route you will travel: %.1f km", distanceKm(coordinate.latitude, coordinate.longitude, &totalDistance, numWayPoints));
- free(coordinate.latitude);
- free(coordinate.longitude);
- getchar();
- return;
- }
- double distanceKm(const double *latitude, const double *longitude, double *totalDistance, int numWayPoints)
- {
- for (int i = 0; i < numWayPoints - 1; i++)
- {
- *totalDistance += 6378.388 * acos(sin((PI / 180) *latitude[i]) * sin((PI / 180) *latitude[i + 1]) + cos((PI / 180) *latitude[i]) * cos((PI / 180) *latitude[i + 1]) * cos((PI / 180) *(longitude[i] - longitude[i + 1])));
- }
- return *totalDistance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement