Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There four problems with the implementation of your code:</p> <ol> <li>It doesn't copy the elements of the list.</li> <li>It doesn't assign the value of <code>new_list</code> to the <code>list</code> variable in <code>main</code></li> <li>It inserts values from the back to the front, instead of after the last value</li> <li><code>max_size</code> doesn't get updated. It's easy to miss this, because you only increase the size of the array by one each time. That way it would need to allocate each time a value is added. If you increase the new size by more then one it will still reallocate every time.</li> </ol> <p>The first problem can be fixed by changing the for loop in <code>list_add</code> so it makes a copy:</p> <pre><code>for (int i = 0; i &lt; space_used; i++) { // this also changed. list_new[i] = list[i]; cout ... } // insert the new value (in the front?) list_new[max_size-space_used-1] = value; delete [] list; // Delete the list afterwards instead of earlier. </code></pre> <p>The second problem can by fixed by returning a pointer to the list. Change the <code>main</code> function to this:</p> <pre><code>for (int i = 0; i &lt; 11; i++) { list = list_add(list, used, N, i); } </code></pre> <p>The third problem can be fixed by changing this line</p> <pre><code>list_new[max_size-space_used-1] = value; </code></pre> <p>to</p> <pre><code>list_new[space_used++] = value; </code></pre> <p>You should also remove the <code>space_used++</code> after this.</p> <p>To see the fourth problem you should change this line</p> <pre><code>int new_max_size = space_used+1; </code></pre> <p>to </p> <pre><code>int new_max_size = space_used+3; </code></pre> <p>It will still reallocate every time. It should however reallocate only two times.</p> <hr> <p>This is the full code:</p> <pre><code>#include &lt;iostream&gt; using std::cout; using std::endl; int* list_add(int *list, int&amp; space_used, int&amp; max_size, int value) { if (max_size - space_used &gt; 0) { list[space_used++] = value; return list; } else { cout &lt;&lt; "Increasing size of array!" &lt;&lt; endl; int new_max_size = space_used+1; int *list_new = new int[new_max_size]; for (int i = 0; i &lt; space_used; i++) { list_new[i] = list[i]; cout &lt;&lt; list_new[i] &lt;&lt; ". "; } cout &lt;&lt; endl; list_new[space_used++] = value; max_size=new_max_size; delete [] list; return list_new; } } int main() { int N = 7; //declaring dynamic array allocation int* list = new int[N]; int used = 0, a_val; for (int i = 0; i &lt; 11; i++) { list=list_add(list, used, N, i); } cout &lt;&lt; endl &lt;&lt; "Storlek: " &lt;&lt; N &lt;&lt; endl &lt;&lt; endl; cout &lt;&lt; "Printar listan " &lt;&lt; endl; for (int i = 0; i &lt; used; i++) { cout &lt;&lt; list[i] &lt;&lt; ". "; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload