Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- int main()
- {
- std::size_t num_of_elements;
- std::cout << "Desired vector size: ";
- std::cin >> num_of_elements;
- std::vector<int> vec(num_of_elements);
- std::cout << "Vector size is " << vec.size() << std::endl;
- std::cout << "Value for each element:" << std::endl;
- for (std::size_t i { 0 }; i < vec.size(); ++i)
- { // using array [] indexing syntax
- std::cout << "vec[" << i << "] = ";
- std::cin >> vec[i];
- }
- int sum { 0 }; // calculated via ranged for
- for (auto& element : vec) sum += element;
- std::cout << "The sum the vector is " << sum << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement