#include // A note on spelling: // // This course generally uses American spelling in its // instructional material. Thus, we use "kilometer" rather // than the Canadian spelling "kilometre." In engineering, // you will frequently communicate with an international // audience, so consistency and clarity are important. // The problem: // // Ask the user to enter a non-negative integer distance // in meters and round that distance to the nearest // kilometer. // // If the user enters a negative distance, immediately // terminate the program by returning 0 from main(). // // Usually, a remainder of less than 500 m means rounding // down, while a remainder greater than 500 m means // rounding up. // // If the distance is exactly halfway between two // kilometers, that is, the remainder is exactly 500 m, // round to the nearest EVEN number of kilometers. // // For example: // // 1400 m -> 1 km // 1600 m -> 2 km // 2500 m -> 2 km // 3500 m -> 4 km // 4500 m -> 4 km // 5500 m -> 6 km // // This method is called "round half to even." You may // also have heard the rule: // // "Even, leave it; odd, raise it." // // Print the resulting number of kilometers. // Function declarations int main(); // Function definitions int main() { return 0; } // Hint: // // Begin by using integer division and the remainder // operator to determine: // // 1. the whole number of kilometers, and // 2. the number of meters remaining. // // What should you do when the remainder is // // less than 500? // greater than 500? // exactly 500? // Here is one solution: /* int main() { int distance_m{}; std::cout << "Enter a distance in meters: "; std::cin >> distance_m; if ( distance_m < 0 ) { return 0; } // Integer division gives the whole number of // kilometers. int distance_km{ distance_m/1000 }; // The remainder gives the number of meters beyond // that whole number of kilometers. int remainder_m{ distance_m%1000 }; if ( remainder_m > 500 ) { // The distance is closer to the next kilometer, // so round up. ++distance_km; } else if ( remainder_m == 500 ) { // The distance is exactly halfway between two // kilometers. Round to the even number of // kilometers. if ( distance_km%2 == 1 ) { ++distance_km; } } std::cout << "The rounded distance is " << distance_km << " km." << std::endl; return 0; } */