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
Want to see the full answer?
Check out a sample textbook solutionChapter 16 Solutions
Starting Out with C++: Early Objects (9th Edition)
Additional Engineering Textbook Solutions
Modern Database Management
Electric Circuits. (11th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Java: An Introduction to Problem Solving and Programming (8th Edition)
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
- In C programming language Question (Strings) Write a function find_Substring() that finds a substring into a string. Pass array s1 and s2 to this function and prints if the substring is present or not. Expected Output 1: Enter string This is a javascript Enter substring script The substring is present Expected Output 2: Enter string This is a javascript Enter substring Jscript The substring is not presentarrow_forwardC++ Coding Write a program that asks a teacher to enter the student's name and the grades received on her quarterly exams. The datatype for the student's name could be a string and the four grades should be entered in an array of data types int. Test scores are on the base of 100. REMINDER: sum and cap CANNOT be integers. The main() functions describe the average function with the following title line: double avg( int grades[], int cap ) The program prints the student's name, her four grades, and her average grades The main() function also uses the following print function: void print( int grades[], int cap);arrow_forwardmplement a function gct according to these guidelines: The function accepts a single positive integer which represents a number of tablespoons of a liquid. You may assume that this number is always between 0 and 25599 (inclusive of both). The function returns a formatted str containing an equivalent number of gallons, cups andtablespoons. The answer should use as many gallons, then cups as possible. For example:gct(312) would return '01g,03c,08t' as 312 tablespoons is equivalent to 1 gallon, 3 cups, and 8 tablespoons. Each of the output quantities (gallons, cups, tablespoons) should occupy two digits. If the values have less than two digits, they should be padded with leading zeroes. I.e., 2 cups is represented by '02c' .arrow_forward
- C++ Format Please Write a function (named "Lookup") that takes two const references to vectors. The first vector is a vector of strings. This vector is a list of words. The second parameter is a list of ints. Where each int denotes an index in the first vector. The function should return a string formed by concatenated the words (separated by a space) of the first vector in the order denoted by the second vector. Input of Test Case provided in PDF:arrow_forwardTreasure Hunter description You are in front of a cave that has treasures. The cave canrepresented in a grid which has rows numbered from 1to , and of the columns numbered from 1 to . For this problem, define (?, )is the tile that is in the -th row and -column.There is a character in each tile, which indicates the type of that tile.Tiles can be floors, walls, or treasures that are sequentially representedwith the characters '.' (period), '#' (hashmark), and '*' (asterisk). You can passfloor and treasure tiles, but can't get past wall tiles.Initially, you are in a tile (??, ). You want to visit all the treasure squares, andtake the treasure. If you visit the treasure chest, then treasurewill be instantly taken, then the tile turns into a floor.In a move, if you are in a tile (?, ), then you can move tosquares immediately above (? 1, ), right (?, + 1), bottom (? + 1, ), and left (?, 1) of thecurrent plot. The tile you visit must not be off the grid, and must not be awall patch.Determine…arrow_forwardProgramming language c++arrow_forward
- Probelm part 1: Define a function called common_divisors that takes two positive integers m and n as its arguments and returns the list of all common divisors (including 1) of the two integers, unless 1 is the only common divisor. If 1 is the only common divisor, the function should print a statement indicating the two numbers are relatively prime. Otherwise, the function prints the number of common divisors and the list of common divisors. Complete each to do with comments to expalin your process. Problem part 2: complete the following todo's and make comments to explain each step # TODO: Define the function 'common_divisors' with two arguments 'm' and 'n'. # TODO: Start with an empty list and append common divisors to the list. # TODO: Create a conditional statement to print the appropriate statement as indicated above. Problem part 3: Call the function common_divisors by passing 5 and 38. Your output should look like this: 5 and 38 are relatively prime. # TODO: Call…arrow_forwardC++ Vectors Help: write a program using parallel vectors and a function which fills each of them with 500 random numbers between 1 and 100. The program should then pass both vectors to a function which will return an integer indicating a count of how many times both vectors had even numbers in the same location. So if vector01[0] contained 4 and vector02[0] contained 12, you would add one to count.If vector01[1] contained 3 and vector02[1] contained 4, you would not add one to count. main would display something like : The Vectors contain 128 cells where both values are even. Above was the question: I already have the code written: #include<iostream> #include<vector> #include<cstdlib> #include<ctime> using namespace std; int main() { int even = 0; srand(time(0)); vector <int> vector01; vector <int> vector02; for (int i = 0; i < 500; i++) { vector01.push_back(rand() % 100 + 1); vector02.push_back(rand() % 100 + 1); if (vector01[i] % 2 == 0…arrow_forwardDart game: Dart location Write a function called DartNumbers() that takes the location of darts thrown as an input, and finds the number of darts that hit the regions yellow, red, blue, and green. A 20x20 square dart board, as shown in the image below, consists of concentric circles with the center at (0,0). The radius of the yellow circle is 3, the red circle is 5, and the blue circle is 10. A dart player throws 10 darts at the board randomly such that any place on the board can be hit with an equal chance. The rand() function can be used to obtain a number with uniform distribution between -10 and 10. Uniform distribution with a given interval means that an equal probability exists to get a number in that interval. Dart Board 10 2. -2 -10 -10 -2 4. 8. 10 The inputs of the function DartNumbers() are: throwX: Indicates the x positions of the darts on the board. throwX is a 1x10 array of random numbers between -10 and 10 with a uniform distribution using the rand function. throw Y:…arrow_forward
- In C++ Create a function that takes in a vector, triples the size of the vector, sets all the values to random numbers, and returns the vector.arrow_forward7- ______ is the alternate of sizeof(); function. a. strtolower(); b. strtoupper(); c. isset(); d. count();arrow_forwardGame of Hunt in C++ language Create the 'Game of Hunt'. The computer ‘hides’ the treasure at a random location in a 10x10 matrix. The user guesses the location by entering a row and column values. The game ends when the user locates the treasure or the treasure value is less than or equal to zero. Guesses in the wrong location will provide clues such as a compass direction or number of squares horizontally or vertically to the treasure. Using the random number generator, display one of the following in the board where the player made their guess: U# Treasure is up ‘#’ on the vertical axis (where # represents an integer number). D# Treasure is down ‘#’ on the vertical axis (where # represents an integer number) || Treasure is in this row, not up or down from the guess location. -> Treasure is to the right. <- Treasure is to the left. -- Treasure is in the same column, not left or right. +$ Adds $50 to treasure and no $50 turn loss. -$ Subtracts…arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning