Angle difference between two bearings: Difference between revisions

Content added Content deleted
m (aligned two statements in the task's prologue.)
(Added C implementation.)
Line 83: Line 83:
1174.84 -154146.66 -161.50
1174.84 -154146.66 -161.50
60175.77 42213.07 37.30
60175.77 42213.07 37.30
</pre>
=={{header|C}}==
This implementation either reads two bearings from the console or a file containing a list of bearings. Usage printed on incorrect invocation.
<lang C>
/*Abhishek Ghosh, 3rd October 2017*/

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

void processFile(char* name){
int i,records;
double diff,b1,b2;
FILE* fp = fopen(name,"r");
fscanf(fp,"%d\n",&records);
for(i=0;i<records;i++){
fscanf(fp,"%lf%lf",&b1,&b2);
diff = fmod(b2-b1,360.0);
printf("\nDifference between b2(%lf) and b1(%lf) is %lf",b2,b1,(diff<-180)?diff+360:((diff>=180)?diff-360:diff));
}
fclose(fp);
}

int main(int argC,char* argV[])
{
double diff;
if(argC < 2)
printf("Usage : %s <bearings separated by a space OR full file name which contains the bearing list>",argV[0]);
else if(argC == 2)
processFile(argV[1]);
else{
diff = fmod(atof(argV[2])-atof(argV[1]),360.0);
printf("Difference between b2(%s) and b1(%s) is %lf",argV[2],argV[1],(diff<-180)?diff+360:((diff>=180)?diff-360:diff));
}

return 0;
}
</lang>
Invocation and output for two bearings :
<pre>
C:\rosettaCode>bearingDiff.exe 29.4803 -88.6381
Difference between b2(-88.6381) and b1(29.4803) is -118.118400
</pre>
File format for bearing list :
<pre>
<Number of records>
<Each record consisting of two bearings separated by a space>
</pre>
Input file :
<pre>
12
20 45
-45 45
-85 90
-95 90
-45 125
-45 145
29.4803 -88.6381
-78.3251 -159.036
-70099.74233810938 29840.67437876723
-165313.6666297357 33693.9894517456
1174.8380510598456 -154146.66490124757
60175.77306795546 42213.07192354373
</pre>
Invocation and output for above bearing list file :
<pre>
C:\rosettaCode>bearingDiff.exe bearingList.txt

Difference between b2(45.000000) and b1(20.000000) is 25.000000
Difference between b2(45.000000) and b1(-45.000000) is 90.000000
Difference between b2(90.000000) and b1(-85.000000) is 175.000000
Difference between b2(90.000000) and b1(-95.000000) is -175.000000
Difference between b2(125.000000) and b1(-45.000000) is 170.000000
Difference between b2(145.000000) and b1(-45.000000) is -170.000000
Difference between b2(-88.638100) and b1(29.480300) is -118.118400
Difference between b2(-159.036000) and b1(-78.325100) is -80.710900
Difference between b2(29840.674379) and b1(-70099.742338) is -139.583283
Difference between b2(33693.989452) and b1(-165313.666630) is -72.343919
Difference between b2(-154146.664901) and b1(1174.838051) is -161.502952
Difference between b2(42213.071924) and b1(60175.773068) is 37.298856
</pre>
</pre>
=={{header|C++}}==
=={{header|C++}}==