3
\$\begingroup\$

This code inserts the number 4 into a vector's even indexes.

vector<double> vecCoeffs;
    // Put the coefficient 4 into each even index and 2 for each odd index
    for(int i = 0; i <= 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;
\$\endgroup\$
  • \$\begingroup\$ The cool (read: inefficient) way is to assign a v[0] = 4 and then for each iteration starting at i=1 assign v[i] = 6 - v[i-1] \$\endgroup\$ – twohundredping Aug 26 '15 at 17:26
4
\$\begingroup\$

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<double> 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 < 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<double> vecCoeffs(11);

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

for (int i = 1; i < vecCoeffs.size() - 1; i += 2) {
    vecCoeffs[i] = 2;                // Odd coefficients
}
for (int i = 2; i < 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<double> vecCoeffs(11);
for (int i = 0; i < 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;
\$\endgroup\$
3
\$\begingroup\$

I think I'd use code more like this:

vecCoeffs.push_back(1);
for (int i=0; i<5; i++) {
    vecCoeffs.push_back(2);
    vecCoeffs.push_back(4);
}
vecCoeffs.push_back(1);

At least to me, this leads to code that's simpler and easier to read: the beginning, some number of repetitions of a pattern, the end. Each easily visible and readable.

If you think/compute/work in terms of the overall size of vecCoeffs, this is somewhat tricky--but if you work with the number of repetitions of the pattern, it's utterly trivial.

As such, if you're starting from the overall size of vector you're going to create, you probably want to compute the number of repetitions separately:

size_t total_size = 11;
size_t repetitions = (total_size-2)/2;

...the loop becomes something like this:

for (int i=0; i<repetitions; i++) {
    vecCoeffs.push_back(2);
    vecCoeffs.push_back(4);
}
\$\endgroup\$
2
\$\begingroup\$

Note: If you have any more that your 11 items then I would use one of the methods by @Jerry or @200. I am not going to reat what they have written so beautifully.

But for so few elements I may just write them out by hand.

vector<double> vecCoeffs = {1, 2, 4, 2, 4, 2, 4, 2, 4, 2, 1};
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.