CJCoding With Joseph
15free / daySign inCreate account

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
Topics:
Templates
📥 Auto-Input:
3\n4 5 6\n2\n1.5 2.25\n
This input is automatically fed to your program's stdin
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.