Robson » Little Red Book » Distance conversion #1

Chapter 1 - Question 7

Given a distance in miles, feet and inches, convert it to a rational number of miles.

Solution 1

C++. Example usage:

C:\code>to_miles 9 10560 0
11
C:\code>to_miles 4.5 6000 50000
6.42551
C:\code>
#include <stdio.h>
#include <stdlib.h>
 
int main(int argc, char *argv[])
{
   if (argc != 4)
       printf("Usage: to_miles [miles] [feet] [inches]");
   else
       printf("%g", atof(argv[1]) + (atof(argv[2]) / 5280) + (atof(argv[3]) / 63360));
}

Log

© 2004-17 robson | cc unless stated