#include // Consider a range of consecutive integers beginning at // 'a' and containing 'b' integers: // // a, a + 1, a + 2, ..., a + b - 1. // // We can describe this using the half-open interval // // [a, a + b). // // The left endpoint is included, while the right endpoint // is not included. // // For example, if // // a = 5 // b = 10, // // then the range is // // 5, 6, 7, 8, 9, 10, 11, 12, 13, 14. // // Similarly, 'c' and 'd' define the range // // [c, c + d). // // Two ranges overlap if there is at least one integer // contained in both ranges. // // Notice that if b == 0, the first range is empty: // // [a, a). // // Similarly, if d == 0, the second range is empty. // An empty range cannot overlap another range. // // Write two conditions: // // 1. The first should determine whether the ranges // overlap. // // 2. The second should determine whether the ranges // do not overlap. // // Do not use the logical NOT operator (!) in either // condition. // Function declarations int main(); // Function definitions int main() { int a{}; std::cout << "Enter a first integer 'a': "; std::cin >> a; int b{}; std::cout << "Enter a non-negative range 'b': "; std::cin >> b; if ( b < 0 ) { std::cout << "The range must be non-negative." << std::endl; return 0; } int c{}; std::cout << "Enter a second integer 'c': "; std::cin >> c; int d{}; std::cout << "Enter a non-negative range 'd': "; std::cin >> d; if ( d < 0 ) { std::cout << "The range must be non-negative." << std::endl; return 0; } // Enter a condition that is true precisely when // // [a, a + b) // // and // // [c, c + d) // // overlap. // // Do not use the logical NOT operator (!). if ( true ) { std::cout << "The ranges overlap." << std::endl; } else { std::cout << "The ranges do not overlap." << std::endl; } // Now enter a condition that is true precisely when // the two ranges do NOT overlap. // // Again, do not use the logical NOT operator (!). if ( false ) { std::cout << "The ranges do not overlap." << std::endl; } else { std::cout << "The ranges overlap." << std::endl; } return 0; } // Hint: // // First consider the case where both ranges are nonempty. // // The ranges // // [a, a + b) // // and // // [c, c + d) // // overlap precisely when // // a < c + d // // AND // // c < a + b. // // Why? // // The first condition says that the first range begins // before the second range ends: // // a < c + d. // // The second says that the second range begins before the // first range ends: // // c < a + b. // // Both conditions must be true for the ranges to overlap. // // However, remember that if // // b == 0 // // or // // d == 0, // // one of the ranges is empty and therefore the ranges // cannot overlap. // There is another useful way to think about the problem. // // Two nonempty ranges do NOT overlap if one lies entirely // before the other. // // The first range lies before the second if // // a + b <= c. // // The second range lies before the first if // // c + d <= a. // // Thus, the ranges do not overlap if either of these // conditions is true. // // Also remember that the ranges do not overlap if either // range is empty. // Here is one solution: /* // The ranges overlap only if both are nonempty and // each range begins before the other range ends. if ( (b > 0) && (d > 0) && (a < c + d) && (c < a + b) ) { std::cout << "The ranges overlap." << std::endl; } else { std::cout << "The ranges do not overlap." << std::endl; } // The ranges do not overlap if either range is empty // or if one range lies entirely before the other. if ( (b == 0) || (d == 0) || (a + b <= c) || (c + d <= a) ) { std::cout << "The ranges do not overlap." << std::endl; } else { std::cout << "The ranges overlap." << std::endl; } */