Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. //Allocate and free dynamic memory, group data structures and using arrays
  2. #define _CRT_SECURE_NO_DEPRECATE
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #define PI 3.14159265359
  9.  
  10. typedef struct {
  11. double latitudes;
  12. double longitudes;
  13. } geoCoord;
  14.  
  15. double radians(double);
  16. double distanceKm(geoCoord *, double *, int);
  17.  
  18. int main(void)
  19. {
  20. int arraySize = 0;
  21. double totalDistance = 0;
  22. double distance = 0;
  23. geoCoord *coord;
  24.  
  25. printf("Please enter the number of waypoints you like to enter: ");
  26.  
  27. while (1)
  28. {
  29. if (scanf("%d", &arraySize) == 1)
  30.  
  31. break;
  32. else
  33. while (getchar() != '\n')
  34. continue;
  35.  
  36. printf("Try again (expected number >= 0): ");
  37. }
  38. getchar();
  39.  
  40. //using malloc(memory allocation) to specify the number of bytes in memory
  41. coord = (geoCoord *)malloc(arraySize * sizeof(geoCoord));
  42.  
  43. //users shall enter the place of depature and the destinations
  44. printf("Enter number of waypoints as \"<latitude> <longitude>\": ");
  45. for (int i = 0; i < arraySize; i++)
  46. {
  47. while (1)
  48. {
  49. printf("\n Waypoint %d: ", i + 1);
  50.  
  51. //sscanf is used to read formatted input from a string/buffer
  52.  
  53. if (scanf("%lf %lf", &coord[i].latitudes, &coord[i].longitudes) != 2)
  54. {
  55. printf("Invalid input (expected \"<latitude> <longitude>\"): ");
  56. }
  57. else
  58. {
  59. break;
  60. }
  61. while (getchar() != '\n')
  62. continue;
  63. }
  64.  
  65. }
  66. getchar();
  67.  
  68. distance = distanceKm(coord, &totalDistance, arraySize);
  69. printf("\nBy taking this route you will travel %.1lf Km.", distance);
  70.  
  71. //function loop to go through and add up all the way points (can use previous lab module)
  72.  
  73. //Clear memory
  74. free(coord);
  75. getchar();
  76. return 0;
  77. }
  78.  
  79. double distanceKm(geoCoord *coord, double *totalDistance, int arraySize)
  80. {
  81. double deltaX, deltaY, lDistKm;
  82. double latA, lonA, latB, lonB;
  83.  
  84. //for loop to compare previous coordinate to next coordinate
  85. //count initially set to 1 so that element 0 is compared to element 1 etc etc.
  86. //with each loop j increases until it is 7 but comp is always 1 ahead.
  87. for (int j = 0; j < arraySize - 1; j++)
  88. {
  89. latA = coord[j].latitudes;
  90. lonA = coord[j].longitudes;
  91. lonB = coord[j + 1].longitudes;
  92. latB = coord[j + 1].latitudes;
  93.  
  94. //used local distance KM formula
  95. deltaY = 111.3 * fabs((radians(latA - latB)));
  96. deltaX = 111.3 * cos((radians(latA + latB)) / 2) * fabs(radians(lonA - lonB));
  97. lDistKm = sqrt(deltaX * deltaX + deltaY * deltaY) * (180 / PI);
  98.  
  99. //Sets value in pointer
  100. *totalDistance += lDistKm;
  101.  
  102. /*Sum check - uncomment to use*/
  103. // printf("|latA %lf lonA %lf| + |latB %lf lonB %lf| = %.2lf\n", latA, lonA, latB, lonB, *totalDistance);
  104. }
  105. return *totalDistance;
  106. }
  107. double radians(double rad)
  108. {
  109. return(rad * PI / 180);
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement