Question

Write a templated function sumList that will take in an array of any type and 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 <iostream>

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;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;


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

//YOUR_CODE
//Generic Function which finds the maximum element of an array
template <class T>
T sumList(T* arr,int size) {
T sum;
//Iterating over the array to find the maximum element
for(int i=0;i<size;i++)
{
sum+=arr[i];
}
return sum;
}

//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;
}

________________

Output:

CAProgram Files (x86)1Dev-Cpp\MinGW64 bin\TempleteSumOflntegersAndStrings.exe 21 he llothere Process exited after 0.03448 sec

______________Thank You

Add a comment
Know the answer?
Add Answer to:
Write a templated function sumList that will take in an array of any type and the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /* Array expander: Write a function that accepts an int array as argument. The function should...

    /* Array expander: Write a function that accepts an int array as argument. The function should create a new array that is n times the size of the argument array, where n is a user input. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array */ #include <iostream> using namespace std; const int NUM_ELEM...

  • One dimensional array What this code print #include <iostream> using namespace std; int main () {...

    One dimensional array What this code print #include <iostream> using namespace std; int main () { const int SIZE = 7; int numbers [SIZE] = {1, 2, 4, 8): // Initialize first 4 elements cout << “Here are the contents of the array:\n"; for (int index = 0; index < SIZE: index++} cout << numbers[index] << “ “; cout << endl; return 0; }

  • using C++ only. The findFirstZero function is supposed to find the first element in an array...

    using C++ only. The findFirstZero function is supposed to find the first element in an array whose value is zero, and sets the parameter  p to point to that element, so the caller can know the location of that element holding the value zero. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only; you must not change the  main routine below in any way. As a result of your fixing...

  • Write out C++ code for the following: Declare a templated function that has a return type...

    Write out C++ code for the following: Declare a templated function that has a return type of T". It has two parameters: An array of T items named arr, and an integer named size. This function is assumed to work with numerical data types. Within the function, create a new T variable named sum and initialize it to 0. Use a for-loop to go from 0 (inclusive) to size (exclusive), adding each element from arr to the sum. Finally, return...

  • PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw...

    PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an...

    Programming Assignment 7 Implement a function named fibo that will get a 20 element initialized an array of zeros from the main function. It will set the array to the Fibonacci sequence. This sequence starts with 1 and 2 as the first 2 elements and each element thereafter is the sum of the previous two elements. (1, 2, 3, 5, 8, 13…). Add another function named findNum that will get the newly created array by fibo from the main function....

  • c++ Implement the following function: 1) Name: ProcessLine a. Parameters: Int 10 character array line, charc...

    c++ Implement the following function: 1) Name: ProcessLine a. Parameters: Int 10 character array line, charc b. Job: The function will modify line such that every occurrence of c is replaced by a "*". It will also return the count of c in line. The main() function is provided. You only need to implement the function Processline. #include <iostream> #include <string> using namespace std; int main() char line 200), c; int Count; { cout<<"Enter some text: "; cin.get(line, 200); cout<<"Enter...

  • How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using...

    How to initialize a dynamic two-dimensional array with values? (C++) Here's my problem... # include using namespace std; int main() {    // Q#5 - dynamic 2d arrays, indexing via subscript operator, pointer arithmetic    // tic tac toe board is an array of int pointers    // each int pointer in the board points to a row    // declare a pointer to an array of int pointers (i.e. a pointer to a pointer of type int)    const...

  • 5.9.2: Modify a string parameter. C++ Complete the function to replace any period by an exclamation...

    5.9.2: Modify a string parameter. C++ Complete the function to replace any period by an exclamation point. Ex: "Hello. I'm Miley. Nice to meet you." becomes: "Hello! I'm Miley! Nice to meet you!" #include <iostream> #include <string> using namespace std; void MakeSentenceExcited(string& sentenceText) { /* Your solution goes here */ } int main() { string testStr; getline(cin, testStr); MakeSentenceExcited(testStr); cout << testStr; return 0; }

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT