-1

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.

Regards, Herry.

Herry
  • 3
  • 3
  • 1
    You try to loop over 15 elements of a one-element array. That will go out of bounds of the array and lead to *undefined behavior*. If you are new to C++, I recommend you first [find a good beginners book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn the basics. – Some programmer dude Sep 29 '16 at 13:21
  • Hi Joachim. Thanks for your message. Even if I put 15 elements in the array, the program gives the same output. I have updated the question :( – Herry Sep 29 '16 at 13:27
  • I am getting only one value after each loop. please help – Herry Sep 29 '16 at 13:31

1 Answers1

0

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.

  • Thanks Porowns ! Not completely solved. Yes it has printed each value of z for fifteen times. What I want is to print the value saved in z only one time as an element in array. I am getting around 10 values of z one by one separately and I want to add these values to a single array which each element represent 10 different values of z. – Herry Sep 29 '16 at 14:44
  • Is Z only one value or is it {1, 2, 3, 4}? @Herry –  Sep 29 '16 at 14:54
  • Z is individual values of the segment of the image. First, while segmentation, I receive the Z value for one segment that I save in Z then again the loop runs and I get another segment and its Z value. Thus I get only one value of Z at a time. So it is {1, 2, 3, 4} but 1, 2, 3, 4 is the Z values I am getting one after another individually. – Herry Sep 29 '16 at 15:16
  • So, you'll want to be running a loop getting the Z values, AND be filling the array values at the same time. If this is dynamic (for example, you may need an array of 15 or 222), then I'd suggest that you use a vector instead of an array. If this is for an assignment, you'll basically just have to resize your array as you fill it with values of Z. I'd say just look at the for loop I posted above, see how the array is filled, and then run a loop (say 15) getting the values of Z, and then filling the array with that value of Z. BE CAREFUL OF SEGMENTATION FAULTS! :) Keyword : new (for dynamic) –  Sep 29 '16 at 15:23
  • Thanks porowns. You exactly understood well. It is dynamic, so I need to use vectors. Thanks for your suggestion. I am checking now how can I implement the vector. Thanks again. Do you have suggestions on it? – Herry Sep 29 '16 at 15:31
  • http://www.cplusplus.com/reference/vector/vector/ Has all you need to know! You'll be interested in the constructor and the push_back or insert function! Basically, you'll just push_back the Z value to whatever your vector is. Good luck! –  Sep 29 '16 at 15:37
  • Thank you so much ! I will post the solution here. – Herry Sep 29 '16 at 15:39