← Back to STL Containers
Question 509
Sum the Top Two
Sum the Top Two
Write a function int sumTopTwo(stack<int> s) that returns the sum of the two elements nearest the top of the stack. You may assume the stack always has at least 2 elements.
The tests print the returned sum on its own line, for example for a stack holding 1, 4, 5 (5 on top) (see Expected Output).
Do NOT write a main function.Expected Output:
9 32
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL ContainersStacks
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.