Distance converter (arrays with functions)

Task solved by the code

  • Read in N integers from the user, representing distances in kilometers, into an array.
  • Distances are read as integers, while the answers  after the conversion must be decimals.
  • Convert the distances to miles.
  • Find and print the sum of the entered distances.

Notice the following in the code

  • Macros are passed as parameters to functions to support better reusability of these functions
    • They will not be dependent on the macro name
    • They can be reused for different length arrays in the same code
  • Only variables that contain relevant data and arrays are being passed to functions. Local variables and results are declared in the function (they don’t contain any relevant data to be copied to the function, so they would just make our code slower for no reason).
  • All functions are commented.
  • The result is calculated in a function and returned. This helps with reusability (i.e. if you need to find lots of results without print statements cluttering up the output).
  • User must enter the distances as integers, but results after conversion are stored in a float array. Type casting is used for the integer array.
  • Original array is preserved (might be needed for some other task for a more complex task) and results are stored in a new array.
    • Since arrays cannot be returned, the new array is also declared in main and passed to the convert function.