← Back to STL Containers
Question 539
Sum Any Size (Template Parameter)
Sum Any Size (Template Parameter)
Write a function templatetemplate <size_t N> int sumAny(const array<int, N>& a)that returns the sum of all elements of anarray<int, N>for anyN. The test calls it once with anarray<int, 3>holding {1, 2, 3} and once with anarray<int, 6>holding {1, 2, 3, 4, 5, 6}, printing each result on its own line (see Expected Output). Do NOT write a main function.
Expected Output:
6 21 9 -3
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
STL Containersstd::array
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.