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; } } //----------------------------------------------------------------------------
What input do you give, what output do you get, what output do you expect, and what values do you think index will take within the following loop: Code: for (index = MAX_SIZE; index >= 0; index--)
I input a string of numbers ending them with a negative number. My output gives me those numbers, (which I expected and need), but after those the remainder of the space in the array is filled up with a large negative number. I think the number is -8123460 or something like that. The output looks like this: Input: 1 2 -3 Output: 1 2 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 -899837460 The large negatives seem to fill in the space unused by the inputted values, because if I enter 20 values (MAX_SIZE) there are none of those negative numbers.
I'm puzzled by the fact that you loop from MAX_SIZE to zero in PrintList, on two counts: 1. why are you looping from MAX_SIZE instead of length 2. you appear to be getting output in the correct direction, i.e. list[0] first. If you loop from the end to the beginning then you *should* get -899837460 -899837460 ... -899837460 2 1 The reason for the junk values is that you're displaying uninitialised data. If you've entered 1,2 as input, then list[0]=1 and list[1]=2, but list[2] through list[MAX_SIZE-1] just contain whatever was on the stack. LESSON here: always initialise data!
Also another problem with looping from MAX_SIZE is that list[MAX_SIZE] is actually undefined. MAX_SIZE is the number of elements in the array, and the first is at zero, so if MAX_SIZE is 5 then list[0], list[1], list[2], list[3], list[4] are defined, but list[5] isn't. So attempting to use list[MAX_SIZE] will give you undefined behaviour.