#include // THIS IS CHALLENGING // // Given four integers, determine the second-largest value. // // For this exercise, "second-largest" does NOT mean the // second-largest distinct value. Instead, imagine sorting // the four values from largest to smallest. The // second-largest value is simply the second value in that // sorted list. // // For example: // // Values Sorted Second-largest // // 1 1 2 2 2 2 1 1 2 // 1 1 2 3 3 2 1 1 2 // 5 4 1 3 5 4 3 1 4 // 2 5 2 2 5 2 2 2 2 // 1 1 3 1 3 1 1 1 1 // 7 7 7 7 7 7 7 7 7 // 8 8 3 1 8 8 3 1 8 // // Notice in particular that if the largest value appears // more than once, the largest and second-largest values // may be equal. // // Write a program that asks the user for four integers and // prints the second-largest value. // // Do not sort the four values. Instead, keep track of the // largest and second-largest values seen so far. // Function declarations int main(); // Function definitions int main() { int a{}; int b{}; int c{}; int d{}; std::cout << "Enter an integer: "; std::cin >> a; std::cout << "Enter a second integer: "; std::cin >> b; std::cout << "Enter a third integer: "; std::cin >> c; std::cout << "Enter a fourth integer: "; std::cin >> d; // Determine the second-largest value. std::cout << "The second-largest value is " << a << std::endl; return 0; } // Hint: // // Begin with the first two integers. // // Determine which is the largest and which is the // second-largest. Store these in two variables: // // largest // second // // At this point, these variables should satisfy // // largest >= second. // // Now consider the third integer. // // There are three possibilities: // // 1. It is at least as large as 'largest'. // // 2. It is smaller than 'largest' but larger // than 'second'. // // 3. It is no larger than 'second'. // // In which of these cases must 'largest' or 'second' // be changed? // // Once you have correctly processed the third integer, // repeat the same procedure for the fourth integer. // A useful way to think about the algorithm is to // maintain the following statement: // // 'largest' is the largest value seen so far, // and 'second' is the second-largest value seen // so far. // // After processing each new value, this statement // should still be true. // // Such a statement is called an invariant. // Here is one solution: /* int main() { int a{}; int b{}; int c{}; int d{}; int largest{}; int second{}; std::cout << "Enter an integer: "; std::cin >> a; std::cout << "Enter a second integer: "; std::cin >> b; // Initialize 'largest' and 'second' using the first // two values. if ( a >= b ) { largest = a; second = b; } else { largest = b; second = a; } // We now know that 'largest' and 'second' are the // largest and second-largest of the first two values. std::cout << "Enter a third integer: "; std::cin >> c; if ( c >= largest ) { // The old largest becomes the second-largest, // and 'c' becomes the new largest. second = largest; largest = c; } else if ( c > second ) { // The largest remains unchanged, but 'c' // becomes the new second-largest. second = c; } // If neither condition was true, then // // c <= second, // // so neither value needs to change. std::cout << "Enter a fourth integer: "; std::cin >> d; if ( d >= largest ) { // The old largest becomes the second-largest, // and 'd' becomes the new largest. second = largest; largest = d; } else if ( d > second ) { // The largest remains unchanged, but 'd' // becomes the new second-largest. second = d; } // If neither condition was true, then // // d <= second, // // so neither value needs to change. std::cout << "The second-largest value is " << second << std::endl; return 0; } */