HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Populating a vector with alternating numbers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withnumberspopulatingvectoralternating

Problem

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

vector 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;

Solution

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 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 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 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;

Code Snippets

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);
}
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
}
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;

Context

StackExchange Code Review Q#101887, answer score: 4

Revisions (0)

No revisions yet.