Starting Out With C++: Early Objects (10th Edition)
Starting Out With C++: Early Objects (10th Edition)
10th Edition
ISBN: 9780135235003
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 16, Problem 4PC

Sequence Accumulation

Write n function

T accum(vector <T> v)

that forms and returns the “sum” of all items in the vector v passed to it. For example, if 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 returned.

NOTE: For any type T, the expression T() yields the value or object created by the default constructor. For example, T() yields the empty string object if T is the string class. If T represents a numeric type such as 1nt, then T() yields 0. Use this fact to initialize your “accumulator.”

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

Blurred answer
Students have asked these similar questions
C++ Format Please Write a function called SortByUpper, that takes a vector of strings and changes that vector to be sorted according to only the uppercase letters in each string. No other characters should be involved in determining the order of the vector. For instance "zCAxT" < "aDyOG", because "CAT" < "DOG". No loops just STL alogorithms and iterators
C++ Help Integers are read from input and stored into a vector until 0 is read. Output the elements from index n to the last index of the vector, where n is specified by the vector's last element. End each number with a newline. Ex: If the input is 7 12 18 2 0, the vector's last element is 2. Thus, the output is: 18 2 Note: The input has at least three integers, and the vector's last element is always less than the vector's size. #include <iostream>#include <vector>using namespace std; int main() {   vector<int> intVect;   int value;   int i;   int n;    cin >> value;   while (value != 0) {      intVect.push_back(value);      cin >> value;   }   // insert code here    return 0;}
/*          Movie List Example           --Showing how to use vectors and structures */ #include <iostream> #include <iomanip> #include <string> #include <vector>   using namespace std;   // define a struct for a Movie object struct Movie               // It is common for the struct name to be capitalized {     string title = "";     // First member of structure - and initialized     int year = 0;          // Second member of structure - and initialized };   int main()  {     cout << "The Movie List program\n\n"          << "Enter a movie...\n\n";       // get vector of Movie objects     vector<Movie> movie_list;               char another = 'y';       while (tolower(another) == 'y')      {         Movie movie;  // make temporary new (initialized) Movie object           cout << "Title: ";         getline(cin, movie.title);           cout << "Year: ";         cin >> movie.year;           movie_list.push_back(movie);…

Chapter 16 Solutions

Starting Out With C++: Early Objects (10th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
1.1 Arrays in Data Structure | Declaration, Initialization, Memory representation; Author: Jenny's lectures CS/IT NET&JRF;https://www.youtube.com/watch?v=AT14lCXuMKI;License: Standard YouTube License, CC-BY
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License