Herry Herry - 1 year ago 82
C++ Question

Adding an element in array after the value obtained dynamically from loop

I am new to C++. I am working in computer vision and I have a potential problem creating the arrays.

I have a loop, from which I am getting a value of the variable (e.g. depth of the specific point on the segmented point-cloud). By this loop, I am getting individual values of this clusters dynamically.

Now I want to store these values in one array.

my code currently saving the value of the variable in 15 elements of array. e.g. z = {1.3, 0, 0, 0, 0, 0, ..} while I want to save after each segmentation, the obtained values to be added to only one array e.g. z = {2.3, 4.5, 2.3, 6.5, 3.5, ..}

code snippet:

double z = centroid [2];

double array[15] = {z};

for (int i=0; i<15; i++)
std::cout << array[i] << std::endl;

could you please help.


Answer Source

A double will only hold a single value, which makes sense why you are only getting one value on output.

If you want to completely fill an array, you need to use a loop:

for (int i = 0; i < 15; i++)
    array[i] = 5; (or whatever you're trying to add into it)

This will completely fill the array with the value of 5, and you'll get 15 5's when you output the array.

Hopefully that gets you on the right track.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download