Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Write a function T accum(vector v) that forms and returns the \"sum\" of all

ID: 3847134 • Letter: C

Question

C++

Write a function

T accum(vector v)

that forms and returns the "sum" of all items in the vector v passed to it. For example, it T is a numeric type such as int or double, the numeric sum will be returned, and if T represents the STL string type, then the result of concatenation is return.

Test your function with a driver program that asks the user to enter 3 integers, uses accum to compute the sum, and prints out the sum. The program than asks the user to enter 3 strings, uses accum to concatenate the strings, and prints the result

Explanation / Answer

#include <iostream>
#include <vector>
using namespace std;
template <class T>
T accum(vector<T> v) {
T sum;
for(int i=0;i<v.size(); i++) {
sum = sum + v[i];
}
return sum;
}
int main()
{
vector<int> v1;
int a,b,c;
vector<string> v2;
string x,y,z;
cout<<"Enter first int value: ";
cin >> a;
cout<<"Enter second int value: ";
cin >> b;
cout<<"Enter thrid int value: ";
cin >> c;
v1.push_back(a);
v1.push_back(b);
v1.push_back(c);
cout<<"Int Sum is "<<accum(v1)<<endl;
cout<<"Enter first string value: ";
cin >> x;
cout<<"Enter second string value: ";
cin >> y;
cout<<"Enter thrid string value: ";
cin >> z;
v2.push_back(x);
v2.push_back(y);
v2.push_back(z);
cout<<"String Sum is "<<accum(v2)<<endl;
return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                             

sh-4.2$ main                                                                                                                                                                                          

Enter first int value: 1                                                                                                                                                                              

Enter second int value: 2                                                                                                                                                                             

Enter thrid int value:                                                                                                                                                                                

3                                                                                                                                                                                                     

Int Sum is 6                                                                                                                                                                                          

Enter first string value: w                                                                                                                                                                           

Enter second string value: e                                                                                                                                                                          

Enter thrid string value: r                                                                                                                                                                           

String Sum is wer

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote