Find the largest value among four given numbers.
When dealing with more than three values, manual if-else logic becomes complex. This is where nested max() functions or initializer lists shine:
Numbers: a = 10, b = 30, c = 20, d = 25 Step 1: Find max of first pair temp = max(a, b) temp = max(10, 30) = 30 Step 2: Compare temp with c result = max(temp, c) result = max(30, 20) = 30 Step 3: Compare result with d maximum = max(result, d) maximum = max(30, 25) = 30 Final Result: Largest is 30
Accept four integers as input
Print the output as "Largest is ___"
-10^15 <= INPUT <= 10^15
#include <iostream> #include <algorithm> using namespace std; int main() { long long a, b, c, d, maximum, temp; cin >> a >> b >> c >> d; // Step 1: Find max of first two numbers temp = max(a, b); // Step 2: Find max of all four using nested max() maximum = max(max(temp, c), d); // Alternative: Use initializer list (C++11 and later) // maximum = max({a, b, c, d}); // Cleaner and more scalable cout << "Largest is " << maximum; return 0; }
temp
to store intermediate resultsmax()
functionBreaking Down the Solution:
// Given: a=10, b=30, c=20, d=25 // Method shown in solution: temp = max(a, b); // temp = max(10, 30) = 30 maximum = max(max(temp, c), d); // max(max(30, 20), 25) // max(30, 25) = 30
Step-by-Step Evaluation:
temp = max(a, b)
→ Compare first two values, store resultmax(temp, c)
→ Compare stored result with third valuemax(previous_result, d)
→ Compare with fourth valueAlternative Without temp Variable:
// Single line - deeply nested maximum = max(max(max(a, b), c), d); // Reads from inside out: // 1. max(a, b) - innermost // 2. max(result, c) - middle // 3. max(result, d) - outermost
The cleanest and most scalable solution uses an initializer list:
#include <iostream> #include <algorithm> using namespace std; int main() { long long a, b, c, d; cin >> a >> b >> c >> d; // One-line solution with initializer list long long maximum = max({a, b, c, d}); cout << "Largest is " << maximum; return 0; }
Advantages of Initializer List:
max({a, b, c, d, e, f})
-std=c++11
or laterMethod | Code Complexity | Readability | Scalability | Best For |
---|---|---|---|---|
Manual If-Else | Very High (16+ conditions) | Poor | ❌ Not scalable | Learning only |
Nested max() with temp | Low | Good | ⚠️ Moderate | Understanding steps |
Nested max() single line | Low | Fair | ⚠️ Gets complex | Compact code |
Initializer List | Very Low | Excellent | ✅ Highly scalable | Production code |
Loop with Array | Low | Excellent | ✅ Infinitely scalable | Many values |
For 4 numbers, manual comparison requires checking many cases:
// ❌ NOT RECOMMENDED: Too complex and error-prone! if (a > b && a > c && a > d) maximum = a; else if (b > a && b > c && b > d) maximum = b; else if (c > a && c > b && c > d) maximum = c; else maximum = d; // This only works if no values are equal! // Edge cases with equal values would need more conditions!
Problems with this approach:
Key Lesson: This is exactly why we use library functions like max()
- they handle all edge cases correctly and make code readable!
For ultimate flexibility, use an array and loop:
#include <iostream> using namespace std; int main() { long long numbers[4]; // Read all four numbers for (int i = 0; i < 4; i++) { cin >> numbers[i]; } // Find maximum using loop long long maximum = numbers[0]; for (int i = 1; i < 4; i++) { if (numbers[i] > maximum) { maximum = numbers[i]; } } cout << "Largest is " << maximum; return 0; }
Benefits:
Evolution of the Problem:
Key Programming Insight:
This progression teaches an essential lesson: as problems scale, we must move from manual, explicit logic to algorithmic, pattern-based solutions. This is the foundation of efficient programming!