x.Hmt trying to simplify this code down if I can, the objective was to \" Displa
ID: 3615547 • Letter: X
Question
x.Hmt trying to simplify this code down if I can, the objective was to "Display the distance and azimuth between city pairs". This was the only way I could get my C program to sort of work. All I did was pretty much Copy the main function and paste it with minor tweeks for the othersAny help is appreciated and thank you in advance! 'Lifesaver' to whoever can help me simplify it!
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
# define PI 3.14159
/*These are the coordinates provided to analyze.
San Antonio 29:25:51N 098:29:36W Cairo 30:02:00N 031:17:00E
San Francisco 37:47:48N 122:24:57W Honolulu 21:16:25N 157:49:13W
Honolulu 21:16:25N 157:49:13W San Francisco 37:47:48N 122:24:57W
Paris 48:52:00N 002:20:00E Barcelona 41:23:00N 002:11:15E
Barcelona 41:23:00N 002:11:15E Paris 48:52:00N 002:20:00E
North Pole 90:00:00N 000:00:00E South Pole 90:00:00S 000:00:00E
Vaitupua Samoa 13:30:00S 172:47:00W Ytridalur Iceland 65:46:00N 016:05:00W
Quito Ecuador 00:13:00S 078:39:00W Djibouti 11:31:00N 043:00:00E
Fake Spot A 00:00:00N 020:00:00W Fake Spot B 00:00:00N 010:00:00W
Fake Spot B 00:00:00N 010:00:00W Fake Spot A 00:00:00N 020:00:00W
*/
float long1,lat1,long2,lat2,longdif,pol1,pol2,distancerad,distancedeg,azimuth;
float latA,latB;
float lonA,lonB;
float difference;
float pol1,pol2;
float RadDist,DegDist;
float azimuth;
int main()
{ latA = 29+(float)25/60+(float)51/3600;
lonA = 98+(float)29/60+(float)36/3600;
latB = 30+(float)02/60+(float)00/3600;
lonB = 31+(float)17/60+(float)00/3600;
difference = lonA+lonB;
pol1 = 90 - lat1;
pol2 = 90 - lat2;
lonA = lonA * PI /180;
latA = latA * PI /180;
lonB = lonB * PI /180;
latB = latB * PI /180;
difference = difference * PI /180;
pol1 = pol1 * PI /180;
pol2 = pol2 * PI /180;
RadDist = cos(pol1) *cos(pol2) + sin(pol1) *sin(pol2) *cos(difference);
RadDist= acos(RadDist);
DegDist = RadDist *180/PI;
DegDist = 40074 * DegDist/360;
printf("Distance between San Antonio and Cairo is = %f ",DegDist);
azimuth = RadDist;
azimuth = sin(difference)*sin(pol1)/sin(RadDist);
azimuth = asin(azimuth);
azimuth = azimuth*180/PI;
printf("Azimuth angle between San Antonio and Cairo is = %f ",azimuth);
latA = 37+(float)47/60+(float)48/3600;
lonA =122+(float)24/60+(float)57/3600;
latB = 21+(float)16/60+(float)25/3600;
lonB = 157+(float)49/60+(float)13/3600;
difference = (lonA-lonB) * -1;
printf("%f ",difference);
pol1 = 90 - latA;
pol2 = 90 - latB;
lonA = lonA * PI /180;
latA = latA * PI /180;
lonB = lonB * PI /180;
latB = latB * PI /180;
difference = difference * PI /180;
pol1 = pol1 * PI /180;
pol2 = pol2 * PI /180;
RadDist = cos(pol1) *cos(pol2) + sin(pol1) *sin(pol2) *cos(difference);
RadDist= acos(RadDist);
DegDist = RadDist *180/PI;
DegDist = 40074 * DegDist/360;
printf("Distance between San Franciso and Honolulu is = %f ",DegDist);
azimuth=sin(longdif)*(sin(pol1)/sin(RadDist));
azimuth = asin(azimuth);
azimuth = 360 - (azimuth*180/PI) ;
printf("Azimuth angle between San Franciso and Honolulu is = %f ",azimuth);
latA = 21+(float)16/60+(float)25/3600;
lonA = 157+(float)49/60+(float)13/3600;
latB = 37+(float)47/60+(float)48/3600;
lonB = 122+(float)24/60+(float)57/3600;
difference = lonA - lonB ;
printf("%f ",difference);
pol1 = 90 - latA;
pol2 = 90 - latB;
lonA = long1 * PI /180;
lat1 = lat1 * PI /180;
lonB = long2 * PI /180;
lat2 = lat2 * PI /180;
difference = difference * PI /180;
pol1 = pol1 * PI /180;
pol2 = pol2 * PI /180;
RadDist = cos(pol1) *cos(pol2) + sin(pol1) *sin(pol2) *cos(difference);
RadDist = acos(RadDist);
DegDist = RadDist *180/PI;
DegDist = 40074 * DegDist/360;
printf("Distance between Honolulu and San Franciso is = %f ",DegDist);
azimuth = RadDist;
azimuth = sin(difference)*sin(pol1)/sin(RadDist);
azimuth = asin(azimuth);
azimuth = azimuth*180/PI ;
printf("Azimuth angle between Honolulu and San Franciso is = %f ",azimuth);
latA = 48+(float)52/60+(float)00/3600;
lonA = 002+(float)20/60+(float)00/3600;
latB = 41+(float)23/60+(float)00/3600;
lonB = 002+(float)11/60+(float)15/3600;
difference = lonA - lonB;
pol1 = 90 - latA;
pol2 = 90- latB;
long1 = long1 * PI /180;
lat1 = lat1 * PI /180;
lonB = lonB * PI /180;
latB = latB * PI /180;
difference = difference * PI /180;
pol1 = pol1 * PI /180;
pol2 = pol2 * PI /180;
RadDist = cos(pol1) *cos(pol2) + sin(pol1) *sin(pol2) *cos(difference);
RadDist = acos(RadDist);
DegDist = RadDist *180/PI;
DegDist = 40074 * DegDist/360;
printf("Distance between Paris and Barcelona is = %f ",DegDist);
azimuth = RadDist;
azimuth = sin(difference)*sin(pol1)/sin(RadDist);
azimuth = asin(azimuth);
azimuth = asin(azimuth);
azimuth = azimuth*180/PI+180 ;
printf("Azimuth angle between Paris and Barcelona is = %f ",azimuth);
latA = 41+(float)23/60+(float)00/3600;
lonA = 002+(float)11/60+(float)15/3600;
latB = 48+(float)52/60+(float)00/3600;
lonB = 002+(float)20/60+(float)00/3600;
difference = lonA - lonB;
pol1 = 90 - lat1;
pol2 = 90 - lat2;
lonA = lonA * PI /180;
latA = latA * PI /180;
lonB = lonB * PI /180;
latB = latB * PI /180;
difference = difference * PI /180;
pol1 = pol1 * PI /180;
pol2 = pol2 * PI /180;
RadDist = cos(pol1) *cos(pol2) + sin(pol1) *sin(pol2) *cos(difference);
RadDist = acos(RadDist);
DegDist = RadDist *180/PI;
DegDist = 40074 * DegDist/360;
printf("Distance between Barcelona and Paris is = %f ",DegDist);
azimuth = RadDist;
azimuth = sin(difference)*sin(pol1)/sin(RadDist);
azimuth = azimuth *-1;
azimuth = asin(azimuth);
azimuth = azimuth * 180/PI ;
printf("Azimuth angle between Barcelona and Paris is = %f ",azimuth);
latA = 90+(float)00/60+(float)00/3600;
lonA = 000+(float)00/60+(float)00/3600;
latB = 90+(float)00/60+(float)00/3600;
lonB = 000+(float)00/60+(float)00/3600;
difference = lonA - lonB;
pol1 = 90 + latA;
pol2 = 90 - latB;
lonA = lonA * PI /180;
latA = latA * PI /180
Explanation / Answer
such as float compute_distance (float lat1, floatlon1, float lat2, float lon2) { //Pre : two sets of points passedin //Post : distance between them is computedand returned float distance=0, pol1=0, pol2=0,RadDist=0, DegDist=0; difference = lonA+lonB;//you sure? isn'tthis a sum? pol1 = 90 - lat1; return (DegDist); } this way you need fewer variables local tomain, if you need to change this code, you only have to change itonce.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.