Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're adding numbers in the wrong "direction". To keep the digits in order, you need to multiply your <code>number</code> variable by 10 and then add <code>array[i]</code> instead of multiplying <code>array[i]</code> and adding it to <code>number</code>.</p> <p>You also need to initialize <code>number</code> to zero before you use it, because a variable has a random value until it's explicitly given one.</p> <p>You also need to do <code>i &lt; 3</code> ("loop while <code>i</code> is less than three") instead of <code>3 &lt; i</code> ("loop while 3 is less than <code>i</code>", which is never true).</p> <pre><code>int array[] = {1,2,3}; int number = 0; for (int i = 0; i &lt; 3; i++) { number *= 10; number += array[i]; } cout &lt;&lt; number; </code></pre> <p>Let's walk through what happens.</p> <ul> <li>In the beginning, <code>number</code> is equal to zero.</li> <li>We get to the loop. First, <code>i</code> equals 0. <ul> <li>We multiply <code>number</code> by 10. For the first iteration, <code>number</code> is still zero after that.</li> <li>We add <code>array[0]</code> (1) to <code>number</code>. <code>Number</code> is now 1.</li> </ul></li> <li><code>i</code> now increments and is equal to 1. 1 is less than 3, so we go in the loop body again. <ul> <li>We multiply <code>number</code> again by 10 to make room for the next digit. <code>number</code> is now equal to 10.</li> <li>We add <code>array[1]</code> (2) to <code>number</code>. <code>Number</code> is now 12.</li> </ul></li> <li><code>i</code> increments and is equal to 2. 2 is less than 3, so we repeat. <ul> <li>We multiply <code>number</code> by 10, again to make room for the next digit. It's now 120.</li> <li>We add <code>array[2]</code> (3) to <code>number</code>, making it 123, the desired result.</li> </ul></li> <li><code>i</code> increments and becomes 3. 3 is obviously not less than 3, so we exit the loop.</li> <li>We print <code>number</code> (123) to the console.</li> </ul>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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