title: C++ : Bizarre Numbers Appearing in Output
body: <p>For my school project, I am supposed to fill an array of 20 with random numbers between (about) -10 and 10.  Then, I have to organize these numbers based on whether they are negative, 0, or positive.  I am told to finish off the program printing out the original array, as well as the new array.</p>
  
  <p>For some reason, the organized array prints out random (TYPE:long) numbers in some of the spaces.  I'm not sure why this is the case.  Below is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;time.h&gt;
  
  int main(int argc, const char * argv[]) {
  // insert code here...
  srand(time(NULL));
  int numbers[20], final[20], first = 1, second = 1;
  
  std::cout &lt;&lt; "Enter 20 numbers, and without sorting, this program will take them and organize them based on positive, negative, and 0.\n\n";
  for(int i = 0; i &lt; 20; i++){
      std::cout &lt;&lt; "Enter number " &lt;&lt; (i+1) &lt;&lt; ": ";
      //std::cin &gt;&gt; numbers[i];
      numbers[i] = (rand()%20 -10);
      std::cout &lt;&lt; numbers[i] &lt;&lt; std::endl;
  }
  
  //Numbers lower than 0
  for(int i = 0; i &lt; 20; i++){
      if(numbers[i] &lt; 0){
          final[i] = numbers[i];
          first++;
      }
  }
  //Numbers equal to 0
  for(int i = first; i &lt; 20; i++){
      if(numbers[i] == 0){
          final[i] = numbers[i];
          second++;
      }
  }
  //Numbers greater than 0
  for(int i = second; i &lt; 20; i++){
      if(numbers[i] == 0){
          final[i] = numbers[i];
      }
  }
  
  std::cout &lt;&lt; "This is your original array: ";
  for(int i = 0; i &lt; 20; i++){
      std::cout &lt;&lt; numbers[i];
      if(i != 19)
          std::cout &lt;&lt; ",";
      std::cout &lt;&lt; " ";
      if(i == 19)
          std::cout &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; std::endl;
  }
  
  std::cout &lt;&lt; "This your new, organized, array: ";
  for(int i = 0; i &lt; 20; i++){
      std::cout &lt;&lt; final[i];
      if(i != 19)
          std::cout &lt;&lt; ",";
      std::cout &lt;&lt; " ";
      if(i == 19)
          std::cout &lt;&lt; std::endl &lt;&lt; std::endl &lt;&lt; std::endl;
  }
  
  system("pause");
  return 0;
  }
  </code></pre>
  
  <p>My output is this:</p>
  
  <pre><code>Enter 20 numbers, and without sorting, this program will take them and organize them based on positive, negative, and 0.
  
  Enter number 1: -6
  Enter number 2: 0
  Enter number 3: -4
  Enter number 4: -5
  Enter number 5: 0
  Enter number 6: -4
  Enter number 7: -5
  Enter number 8: -5
  Enter number 9: -8
  Enter number 10: 5
  Enter number 11: 0
  Enter number 12: -3
  Enter number 13: 5
  Enter number 14: -5
  Enter number 15: 7
  Enter number 16: 2
  Enter number 17: 9
  Enter number 18: 9
  Enter number 19: 3
  Enter number 20: 2
  This is your original array: -6, 0, -4, -5, 0, -4, -5, -5, -8, 5, 0, -3, 5, -5, 7, 2, 9, 9, 3, 2 
  
  
  This your new, organized, array: -6, 1879110449, -4, -5, 0, -4, -5, -5, -8, 1, 0, -3, 1606416384, -5, 1606423158, 32767, 1606416416, 32767, 1606416416, 32767"
  </code></pre>
  
  <p>Thank you in advance to all who reply.  I really appreciate it.</p>