Question: Write a templated function sumList that will take in an array of any type and the length of the array. It should add all the

Write a templated function sumList that will take in an array of any type and the length of the array. It should add all the elements in the array and return the total. Hint: you can declare a variable sum of type T and initialize it like this:

T sum {};

The {} are the best way to make sure that numbers get set to 0, a string gets created as empty etc...

#include

using namespace std;

//Do not modify anything on or above the line below this //YOUR_CODE_BELOW

//YOUR_CODE

//YOUR_CODE_ABOVE //Do not modify anything on or below the line above this

int main() { int nums[] = {4, 2, 8, 7}; string words[] = {"hello", "there"};

cout << sumList(nums, 4) << endl; cout << sumList(words, 2) << endl;

return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!