Time to put your knowledge of vectors and references to work! You'll build a program that manages a collection of integers using a vector and a set of helper functions.
YOUR TASK
Create a program that:
Declares a vector<int> and adds the values 10, 20, 30, 40, 50 using push_back
Writes a function void printAll(const vector<int>& v) that prints each element on its own line
Writes a function void addElement(vector<int>& v, int val) that appends a value
Writes a function int getSum(const vector<int>& v) that returns the sum of all elements
Calls addElement to add 60, then prints all elements, then outputs the sum
Need a hint?
Step-by-step:
1. #include <vector> at the top
2. void printAll(const vector<int>& v) — loop with range-based for
3. void addElement(vector<int>& v, int val) — call v.push_back(val);
4. int getSum(const vector<int>& v) — loop and accumulate into a local int sum = 0
5. In main: build the vector, call addElement(nums, 60), call printAll(nums), then cout << "Sum: " << getSum(nums)
Expected Output:
10 20 30 40 50 60 Sum: 210
Code Editor
Ready
Output
Console
Console initialized...
Ready for Project 2: Dynamic Array Manager
> Test your code with the "Check My Code" button
> When it works, the Complete button will activate!