#include /********************************** * Recall that when you perform * integer division, any fractional * part is discarded. This is * equivalent to truncating the * result toward zero. * * For example, * * 15/7 = 2 * * because 15/7 is approximately * 2.14, and the fractional part * is discarded. * * Can you find an integer * arithmetic calculation that * instead rounds the result up * when both integers are positive? **********************************/ // Function declarations int main(); // Function definitions int main() { int m{ 7 }; // First, observe what ordinary integer division does. // These output 2. std::cout << 14/m << std::endl; std::cout << 15/m << std::endl; std::cout << 20/m << std::endl; // These output 3. std::cout << 21/m << std::endl; std::cout << 22/m << std::endl; std::cout << 27/m << std::endl; // These output 4. std::cout << 28/m << std::endl; std::cout << 29/m << std::endl; std::cout << std::endl; // Now change each calculation below so that the // result is rounded up whenever there is a // nonzero remainder. // // You may use only integer arithmetic. // This should output 2. std::cout << 14/m << std::endl; // These should output 3. std::cout << 15/m << std::endl; std::cout << 20/m << std::endl; std::cout << 21/m << std::endl; // These should output 4. std::cout << 22/m << std::endl; std::cout << 27/m << std::endl; std::cout << 28/m << std::endl; // This should output 5. std::cout << 29/m << std::endl; return 0; } // Hint: // // Consider a simpler example in which the divisor is 5. // // What is n/5 for each of the following positive // integer values of n? // // n: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // // Can you see how you could modify 'n' before dividing // by 5 to produce the values you want? // Here is the pattern produced by ordinary integer division: // // n: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // n/5: 0 0 0 0 1 1 1 1 1 2 2 2 2 2 3 3 3 3 // // We instead want: // // n: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // result: 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 // // Notice that the desired pattern is shifted relative // to the pattern produced by ordinary integer division. // // Note: We are only asking for a formula that works // when both integers are positive. /* * Solution: * * Replace n/m with * * (n - 1)/m + 1 * * For positive integers 'n' and 'm', this performs * integer division but rounds the result up whenever * there is a nonzero remainder. * * For example, when m = 7: */ /* int main() { int m{ 7 }; // This should output 2. std::cout << ((14 - 1)/m + 1) << std::endl; // These should output 3. std::cout << ((15 - 1)/m + 1) << std::endl; std::cout << ((20 - 1)/m + 1) << std::endl; std::cout << ((21 - 1)/m + 1) << std::endl; // These should output 4. std::cout << ((22 - 1)/m + 1) << std::endl; std::cout << ((27 - 1)/m + 1) << std::endl; std::cout << ((28 - 1)/m + 1) << std::endl; // This should output 5. std::cout << ((29 - 1)/m + 1) << std::endl; return 0; } */ // Another solution is // // (n + m - 1)/m. // // To see why this gives the same result, rewrite the // numerator: // // n + m - 1 = (n - 1) + m. // // Therefore, // // (n + m - 1)/m // // is equivalent, for positive integers, to // // (n - 1)/m + m/m // // which is // // (n - 1)/m + 1. // // Thus, the two formulas produce the same result for // positive integers: // // (n - 1)/m + 1 // // and // // (n + m - 1)/m.