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

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

ID: 3686890 • Letter: W

Question

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.

Can you write in c++ please, that would be greatly appreciated! Thank you.

Explanation / Answer

Hi, i give you some code about your question. In first program define a main function and two other function other function call in main function. First user input 3 integer then enter 3 string.

Integer function return sum of all integer value and sting function concatenate the strings value.

In Second program define main function and print all vector value. And also print all value as a string

First program start here

#include <iostream>

#include <vector>

#include <numeric>

#include <string>

using namespace std;

std::vector<int> v;

std::vector<string> s;

//this function use for return integer vector sum value

int assert_choice()

{

    int sum_of_elems = 0;

    cout << "extended vector size = " << v.size() << endl;

    

    sum_of_elems = std::accumulate(v.begin(), v.end(), 0);

    return sum_of_elems;

}

//this function use for return string vector sum value

string choice()

{

    cout << "extended vector size = " << s.size() << endl;

    string a = accumulate( s.begin(), s.end(), string("") );

    return a;

}

int main()

{

   int i;

   int a;

   string name;

   // ask to user enter 3 intager value and push 3 values into the vector

   for(i = 1; i < 4; i++){

      cout << "Enter your value " << i << ": " << flush;

      cin >> a;

      v.push_back(a);

   }

   // ask to user enter 3 string value and push 3 values into the vector

   for(i = 1; i < 4; i++){

      cout << "Enter your string " << i << ": " << flush;

      cin >> name;

      s.push_back(name);

   }

   //call intager vector function

   int total = assert_choice();

   cout << "Sum of intager value " << total << ": " << flush;

   //call string vector function

   string concats = choice();

   cout << "concat string value " << concats << ": " << flush;

   return 0;

}

First program end here

Second program start here. Please run this program with gcc 5.3

#include <iostream>

#include <vector>

#include <numeric>

#include <string>

#include <functional>

int main()

{

    std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    int sum = std::accumulate(v.begin(), v.end(), 0);

    int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());

    std::string s = std::accumulate(std::begin(v), std::end(v), std::string{},

                                   [](const std::string& a, int b) {

                                        return a.empty() ? std::to_string(b)

                                               : a + ' ' + std::to_string(b);

                                    });

    std::cout << "sum: " << sum << ' '

              << "product: " << product << ' '

              << "dash-separated string: " << s << ' ';

}

Second program end here

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