I can't figure out what I am doing wrong with this program. When I print the array it outputs a bunch of random large negative numbers and then the proper numbers. Please help me!

Code:

#include <iostream> //for cin, cout #include <conio.h> //for _getch() using namespace std; const int MAX_SIZE = 20; // maximum size of array void ReadList( int list[], // array of nonnegative integers int& length); // number of nonnegative integers // in the array //---------------------------------------------------------------------------- //Purpose: To read nonnegative integers into an array //Precondition: None //Postcondition: length has been assigned the number of nonnegative integers read // and stored in list length >0 // list[0] through list[length - 1] have been filled with nonnegative // integers entered from the keyboard //------------------------------------------------------------------------------ void PrintList(const int list[], // array of nonnegative integers int length); // number of nonnegative integers // in the array //--------------------------------------------------------------------------- //Purpose: To print the nonnegative integers stored in the array list //Precondition: length is the number of nonnegative integers in list. // list[0] through list[length-1] are assigned nonnegative integers //Postcondition: nonnegative integers in list[0] through list[length-1] have been // printed //----------------------------------------------------------------------------- int main() { int list[MAX_SIZE]; // array of nonnegative integers int length; // number of nonnegative integers in array //Call the functions ReadList(list, length); PrintList(list, length); _getch(); return 0; } //----------------------------------------------------------------------------- void ReadList(int list[], // array of nonnegative integers int& length) // number of nonnegative integers // in the array //Purpose: To read nonnegative integers into an array { int number; int index = 0; cout << "Enter nonnegative integers each separated by a blank space,\n" << "and mark the end of the list with a negative number: "; cin >> number; //read the first integer entered //check that the number is nonnegative and // the size of the array is not exceeded while ((number >=0) && (index < MAX_SIZE)) { list[index] = number; //store the integer in the array index++; // increment the index cin >> number; // read the next integer } length = index; // length is the number of nonnegative integers // in the list } //---------------------------------------------------------------------------- void PrintList(const int list[], int length) { int index; cout << "\nThe list contains " << length << " nonnegative integer(s) as follows: \n"; for (index = MAX_SIZE; index >= 0; index--) { cout << list[index] << endl; } } //----------------------------------------------------------------------------

*Last edited by shabbir; 12Nov2010 at 08:19.. Reason: Code blocks*