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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
/** * File: dist_converter.c * Author: Risto Heinsar * Created: 26.10.2020 * Last edit: 04.10.2023 * * Description: Reads an array filled with integers (representing distances * in kilometers) and converts it to miles. * Prints the sum of all distances in miles. */ #include <stdio.h> #define DIST_COUNT 4 #define MILES_IN_KM 1.609f void ReadDistances(int dist[], int n); void ConvertArrToMiles(int distKm[], float distMi[], int n); void PrintDistances(float dist[], int n); float SumOfAllDist(float dist[], int n); int main(void) { int distancesInKilometers[DIST_COUNT]; float distancesInMiles[DIST_COUNT], totalDist; ReadDistances(distancesInKilometers, DIST_COUNT); ConvertArrToMiles(distancesInKilometers, distancesInMiles, DIST_COUNT); PrintDistances(distancesInMiles, DIST_COUNT); totalDist = SumOfAllDist(distancesInMiles, DIST_COUNT); printf("The sum of all distances is %.2f miles!\n", totalDist); return 0; } /** * Description: Asks the user for n integers and stores them in an array. * * Parameters: dist - array to store distances entered by the user * n - number of distances to read (also length of array) * * Return: none */ void ReadDistances(int dist[], int n) { int i; for (i = 0; i < n; i++) { printf("Enter distance %d in km (integer): ", i + 1); scanf("%d", &dist[i]); } } /** * Description: Converts an array filled with distances in kilometers to * miles and stores them in the second array. * * Parameters: distKm - array with distances in kilometers * distMi - array to store calculated distances in miles * n - number of distances to convert (length of both arrays) * * Return: none */ void ConvertArrToMiles(int distKm[], float distMi[], int n) { int i; for (i = 0; i < n; i++) { distMi[i] = (float)distKm[i] / MILES_IN_KM; } } /** * Description: Prints the given array of distances with 2 decimal places * after the comma. * * Parameters: dist - array with distances as decimals * n - length of the array. * * Return: none */ void PrintDistances(float dist[], int n) { int i; printf("The converted distances are: "); for (i = 0; i < n; i++) { printf("%.2f ", dist[i]); } printf("\n"); } /** * Description: Sums up all the distances in the array, returns the sum * * Parameters: dist - array with distances as decimals * n - length of the array. * * Return: the sum of given array members (total distance) */ float SumOfAllDist(float dist[], int n) { int i; float sum = 0; for (i = 0; i < n; i++) { sum += dist[i]; } return sum; } |