Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I haven't looked at your code, but your array must be large enough to contain all the values you will store in it. 100 certainly isn't going to be enough for most input for this problem.</p> <p>E.g. this code..</p> <pre><code>int someArray[100]; someArray[150] = 10; </code></pre> <p>Writes to a location large than the array (150 > 100). This is known as a memory overwrite. Depending on what happened to be at that memory location your program may crash immediately, later, or never at all.</p> <p>A good practice when using arrays is to assert in someway that the element you are writing to is within the bounds of the array. Or use an array-type class that performs this checking.</p> <p>For your problem the easiest approach would be to use the STL vector class. While you must add elements (vector::push_back()) you can later access elements using the array operator []. Vector will also give you the best iterative performance.</p> <p>Here's some sample code of adding the numbers 0-100 to a vector and then printing them. Note in the second loop we use the count of items stored in the vector.</p> <pre><code>#include &lt;vector&gt; // std::vector ... const int MAX_ITEMS = 100; std::vector&lt;int&gt; intVector; intVector.reserve(MAX_ITEMS); // allocates all memory up-front // add items for (int i = 0; i &lt; MAX_ITEMS; i++) { intVector.push_back(i); // this is how you add a value to a vector; } // print them for (int i = 0; i &lt; intVector.size(); i++) { int elem = intVector[i]; // this access the item at index 'i' printf("element %d is %d\n", i, elem); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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