#include // Write a program that queries the user for two // integers 'lower' and 'upper'. // // If lower > upper, return 0. // // Otherwise, query the user for three integer // coefficients 'a', 'b', and 'c' defining the // quadratic polynomial // // 2 // p(n) = a n + b n + c. // // Consider only integer values of 'n' satisfying // // lower <= n <= upper. // // Find the value of 'n' for which p(n) is largest, // and then print both that value of 'n' and the // maximum value of the polynomial. // // If the maximum occurs at more than one value // of 'n', print the smallest such value. // // For example, if // // lower = -3, // upper = 3, // // and // // 2 // p(n) = -2n + 4n + 1, // // then you must calculate p(n) for each integer // n = -3, -2, ..., 3 and determine which value // is largest. // // Do not try to find the maximum using properties // of quadratic functions. The purpose of this // exercise is to find the maximum by examining // each integer value in the specified interval. // Function declarations int main(); // Function definitions int main() { return 0; } // Hint: // // Start by assuming that the maximum occurs at // n = lower. Calculate and store p(lower). // // Then examine each remaining integer from // lower + 1 through upper. Whenever you find // a larger value, remember both the new value // of 'n' and the new maximum. // // Think about why you should use '>' rather // than '>=' when comparing values. // Here is one solution: /* int main() { int lower{}; std::cout << "Enter a lower bound: "; std::cin >> lower; int upper{}; std::cout << "Enter an upper bound: "; std::cin >> upper; if ( lower > upper ) { return 0; } int a{}; std::cout << "Enter the coefficient 'a' of n^2: "; std::cin >> a; int b{}; std::cout << "Enter the coefficient 'b' of n: "; std::cin >> b; int c{}; std::cout << "Enter the constant coefficient 'c': "; std::cin >> c; // Begin by assuming that the maximum occurs // at the first integer in the interval. int n_maximum{ lower }; int p_maximum{ a*lower*lower + b*lower + c }; // We have already examined 'lower', so begin // by examining 'lower + 1'. for ( int n{ lower + 1 }; n <= upper; ++n ) { int p{ a*n*n + b*n + c }; if ( p > p_maximum ) { n_maximum = n; p_maximum = p; } } std::cout << "At n = " << n_maximum << ", the polynomial has a maximum value of " << p_maximum << "." << std::endl; return 0; } */