1.
Fix the code below so that it creates a vector with 5 elements initialized to 1, and changes the third element of that vector to a 2.
Solution.
Below is one way to fix the program. You must always include the
<vector>
header when dealing with vectors. Furthermore, to initialize a vector’s elements to a certain value, you must include that value as a second argument to the size. Finally, vectors are zero-indexed.
#include <iostream>
#include <vector>
using namespace std;
int main () {
vector<int> nums (5, 1);
nums[2] = 2;
}