← Back to STL Containers
Question 516
Sum a Stack Copy
Sum a Stack Copy
Write a function int sumStack(stack<int> s) that returns the sum of every element in the stack. The caller's original stack must be left untouched. An empty stack sums to 0.
The tests print the returned sum on one line, then the size of the original stack after the call on the next line. For a stack holding 4, 5, 6 the output is exactly what is shown in Expected Output.
Do NOT write a main function.Expected Output:
15 3 0 0
= 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.