Search⌘ K
AI Features

Solution: Engineering Unit Converter

Explore how to implement unit conversion functions in C++. Understand defining functions for converting miles to kilometers and Fahrenheit to Celsius, perform calculations using floating-point division, and display the results. This lesson helps you grasp function design, parameter passing, and output formatting within modern C++ programming.

We'll cover the following...
C++ 23
#include <iostream>
// Function to convert miles to kilometers
double milesToKilometers(double miles) {
return miles * 1.60934;
}
// Function to convert Fahrenheit to Celsius
double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32.0) * (5.0 / 9.0);
}
int main() {
double miles = 60.0;
double fahrenheit = 98.6;
// Calling the functions and storing their return values
double kilometers = milesToKilometers(miles);
double celsius = fahrenheitToCelsius(fahrenheit);
std::cout << miles << " miles is " << kilometers << " km.\n";
std::cout << fahrenheit << "F is " << celsius << "C.\n";
return 0;
}
...