title: Lab 3.10.2 Dynamic data – how to obtain it and how to get rid of it
body: <p>So, the problem presented is, </p>
  
  <p>Look at the code below – it's a skeleton of a program operating on the dynamic collection of data. </p>
  
  <p>The idea is to use a structure
  containing two fields: the first stores the number of elements in collections, and the second is the actualcollection (a dynamically
  allocated vector of ints). </p>
  
  <p>As you can see, the collection is filled with the required amount of pseudo-random data.</p>
  
  <p>Unfortunately, the program requires completion, as the most important function, intended to add elements to the collection, is still empty.</p>
  
  <p>Here's what we expect from the function:</p>
  
  <p>if the collection is empty, it should allocate a one-element vector and store a new value in it;</p>
  
  <p>if the collection is not empty, it should allocate a new vector with a length greater by one than the current vector, then copy all
  elements from the old vector to the new one, append a new value to the new vector and finally free up the old vector.</p>
  
  <p>I'm not hoping for the solution or anything but a pointer in the right direction would be greatly appreciated.</p>
  
  <p>I've been tinkering at it for a while, and managed to get it to at least make array larger by one, but it only ever seems to copy over the location of the first value.</p>
  
  <p>My code is just the stuff under AddToCollection, by the by.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  using namespace std;
  struct Collection {
  int elno;
  int *elements;
  };
  void AddToCollection(Collection &amp;col, int element) {
  // Insert your code here
  
  
  if(col.elements != NULL) {
      col.elno = sizeof(col.elements);
      ++col.elno;
      int *jeffry = new int[col.elno + 1];
      jeffry[col.elno] = element;
      for (int f = 0; f &lt; (col.elno - 1); f++) {
          jeffry[f] = col.elements[f];
      }
  
  
      col.elements = jeffry;
  }
  if(col.elements == NULL){
      col.elements =new int[element];
  
      }
  
  }
  
  void PrintCollection(Collection col) {
  cout &lt;&lt; "[ ";
  for(int i = 0; i &lt; col.elno; i++)
      cout &lt;&lt; col.elements[i] &lt;&lt; " ";
  cout &lt;&lt; "]" &lt;&lt; endl;
  }
  
  int main(void) {
  Collection collection = { 0, NULL };
  int elems;
  cout &lt;&lt; "How many elements? ";
  cin &gt;&gt; elems;
  srand(time(NULL));
  for(int i = 0; i &lt; elems; i++)
      AddToCollection(collection, rand() % 100 + 1);
  PrintCollection(collection);
  delete[] collection.elements;
  return 0;
  </code></pre>
  
  <p>}</p>