Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. /*File contents: Travel planning using a structure
  2. Authors: Daniel Dedoukh, Mohamed Moustafa, Momme König*/
  3.  
  4. #define _CRT_SECURE_NO_DEPRECATE
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #define PI 3.14159265
  9.  
  10. double distanceKm(const double *longitude, const double *attitude, double *totalDistance, int numWayPoints);
  11.  
  12. int main(void)
  13. {
  14. //This structure will store longitude and latitude
  15. struct coordinate {
  16. double *latitude;
  17. double *longitude;
  18. } coordinate;
  19.  
  20. int numWayPoints;
  21. double totalDistance = 0.0;
  22.  
  23. printf("Enter number of waypoints: ");
  24.  
  25. // To avoid user input mistakes.
  26. while ((!scanf("%d", &numWayPoints)) || (numWayPoints < 0))
  27. {
  28. char invalid;
  29.  
  30. printf("\nInvalid input: ");
  31. while ((invalid = getchar()) != '\n')
  32. putchar(invalid);
  33. printf("\nTry again: \n");
  34.  
  35. }
  36.  
  37. //Using memory allocation to specify the number of points with respect to user input
  38. coordinate.latitude = (double *)calloc(numWayPoints, sizeof(double));
  39. coordinate.longitude = (double *)calloc(numWayPoints, sizeof(double));
  40.  
  41. printf("\nEnter waypoints as \"<latitude> <longitude>\" separated by space");
  42.  
  43. // ask the user
  44. for (int i = 0, count = 1; i < numWayPoints, count <= numWayPoints; i++, count++)
  45. {
  46. printf("\nWaypoint %d:", count);
  47.  
  48. while (scanf("%lf %lf", &coordinate.latitude[i], &coordinate.longitude[i]) != 2)
  49. {
  50. char invalid;
  51.  
  52. printf("\nInvalid input: ");
  53. while ((invalid = getchar()) != '\n')
  54. putchar(invalid);
  55. printf("\nTry again: \n");
  56. }
  57. getchar();
  58. }
  59.  
  60. printf("By taking this route you will travel: %.1f km", distanceKm(coordinate.latitude, coordinate.longitude, &totalDistance, numWayPoints));
  61.  
  62. free(coordinate.latitude);
  63. free(coordinate.longitude);
  64.  
  65. getchar();
  66. return;
  67. }
  68.  
  69. double distanceKm(const double *latitude, const double *longitude, double *totalDistance, int numWayPoints)
  70. {
  71.  
  72. for (int i = 0; i < numWayPoints - 1; i++)
  73. {
  74. *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])));
  75. }
  76.  
  77. return *totalDistance;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement