94.7k views
5 votes
Writing mathematical functions. C++

Complete the function MilesToKilometers() that takes one integer parameter as a length in miles. The function returns a double as the length converted to kilometers, given that 1 mile = 1.60934 kilometers.
Ex: If the input is 17, then the output is:
Result: 27.3588 kilometers
----------------
#include
using namespace std;

double MilesToKilometers(int userMiles) {

/* Your code goes here */

}

int main() {
int miles;

cin >> miles;

cout << "Result: " << MilesToKilometers(miles);
cout << " kilometers" << endl;

return 0;
}

User Tuwanda
by
7.3k points

1 Answer

2 votes

Answer:

Here is one possible implementation of the MilesToKilometers function in C++:

#include <iostream>

using namespace std;

double MilesToKilometers(int userMiles) {

double kilometers = userMiles * 1.60934;

return kilometers;

}

int main() {

int miles;

cout << "Enter a distance in miles: ";

cin >> miles;

double kilometers = MilesToKilometers(miles);

cout << "Result: " << kilometers << " kilometers" << endl;

return 0;

}

Step-by-step explanation:

In this implementation, the MilesToKilometers function takes an integer parameter userMiles representing the length in miles, and returns a double representing the length in kilometers. The function simply multiplies the input value by the conversion factor of 1.60934 to calculate the length in kilometers.

In the main function, the program prompts the user to enter a distance in miles, reads in the input using cin, calls the MilesToKilometers function to convert the input to kilometers, and then outputs the result as a double followed by the string "kilometers".

User Forrest Keppler
by
7.8k points