Populating a vector with alternating numbers This code inserts the number 4 into a vector's even indexes.

vector&lt;double&gt; vecCoeffs;
    // Put the coefficient 4 into each even index and 2 for each odd index
    for(int i = 0; i &lt;= 10; i++){
        vecCoeffs[i];
        if(i % 2 == 0 ){
            vecCoeffs.push_back(4);
        }else{
            vecCoeffs.push_back(2);
        }
        // Starting and ending with coefficient 1
        vecCoeffs[0] = 1;
        vecCoeffs[10] = 1;
If you know how large the vector needs to be, the specify the size in the constructor so that it doesn't need to guess.

vecCoeffs[i]; is a useless statement.

It would be clearer to pull the assignment of starting and ending coefficients out of the loop, and to avoid assigning vecCoeffs[10] twice.  You could assign the starting and ending coefficients in the same statement.

The if-else would be better as a ternary conditional.

vector&lt;double&gt; vecCoeffs(11);
// Starting and ending coefficients
vecCoeffs[0] = vecCoeffs[vecCoeffs.size() - 1] = 1;
// Put the coefficient 2 into each odd index and 4 into each even index
for (int i = 1; i &lt; vecCoeffs.size() - 1; i++) {
    vecCoeffs[i] = (i % 2 ? 2 : 4);
}


But the modulo operator is relatively slow.  You would be better off with two loops…

vector&lt;double&gt; vecCoeffs(11);

// Starting and ending coefficients
vecCoeffs[0] = vecCoeffs[vecCoeffs.size() - 1] = 1;

for (int i = 1; i &lt; vecCoeffs.size() - 1; i += 2) {
    vecCoeffs[i] = 2;                // Odd coefficients
}
for (int i = 2; i &lt; vecCoeffs.size() - 1; i += 2) {
    vecCoeffs[i] = 4;                // Even coefficients
}


… or maybe just one, but getting the termination correct is trickier.  This version might eliminate an instruction or two from the loop, but I don't recommend it.

vector&lt;double&gt; vecCoeffs(11);
for (int i = 0; i &lt; vecCoeffs.size() - 1; i += 2) {
    vecCoeffs[i] = 4;                // Even coefficients
    vecCoeffs[i + 1] = 2;            // Odd coefficients
}
// Starting and ending coefficients
vecCoeffs[0] = vecCoeffs[vecCoeffs.size() - 1] = 1;