← Back to Templates
Question 749
Sum an Array with a Template
Sum an Array with a Template
Write a function template sumArray(const T values[], int size) that returns the total of the first size elements as a T. Start the total at T{} (the zero value of the type) and add each element with a loop. When size is 0, return that starting total without touching the array.
The tests read a count and that many int values, print their sum, then read a count and that many double values, print their sum, and finally call sumArray on the int array with a size of 0 and print that result too, each on its own line. For the first test the input is 3, then 4 5 6, then 2, then 1.5 2.25.
Do NOT write a main function.
Expected Output:
15 3.75 0
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Templates
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.