Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- // This function returns the sum of a list of numbers.
- int sum(vector<int> list)
- {
- int total = 0; // Start with a total of zero.
- // Go from the beginning to the end of the list.
- for (size_t i = 0; i < list.size(); ++i)
- {
- // Add each number to the sum.
- total += list[i];
- }
- return total;
- }
- int main()
- {
- // Create a list of numbers.
- vector<int> number_list = { 1, 2, 3, 4, 5, 6 };
- // Display the sum of the numbers.
- cout << sum(number_list) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement