[Solved] Adding element from for loop to an array


Since you can use C++, the default option for storing an array of integers is std::vector:

std::vector<int> wave;
for (int i = 0; i <= 4000; i += 100)
    wave.push_back(i);

If you want to have a C array as the result (e.g. for compatibility with other code that uses such arrays), because you know the final size of your array in advance, you better mention the size in the array definition:

int wave[41];
int index = 0;
for (int value = 0; value <= 4000; value += 100)
    wave[index++] = value;

If you didn’t know the final size, and for some reason didn’t want to use std::vector, you’d have to use dynamically-allocated arrays (with malloc or new[]).

3

solved Adding element from for loop to an array