CJCoding With Joseph
15free / daySign inCreate account

Sum a Queue Without Emptying It

Write a function int sumQueue(queue<int> q) that returns the sum of every element in the queue. Return 0 for an empty queue.

Each test prints the returned sum on one line, then the size of the caller's queue on the next line. For a queue holding 4, 5, 6 the output is exactly what is shown in Expected Output.

For an empty queue the output is:
0
0

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
Topics:
STL ContainersQueues
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.