little problem of array stuff

This program gets user to keep entering integers until a negative integer is entered, and print the number of nonnegative integers entered and the list of them in reverse. When I test my program, everything is fine except I get -85693460 on my screen...

Could you guys take a look at my code and explain a bit of what I did wrong with my loop. Because in fact I don't quite understand what's going on with loops combined with arrays. Sorry for my bad English...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>

using namespace std;

const int MAX_SIZE = 20;

void ReadList(int[], int&);

void PrintList(const int[], int);

int main()
{
	int list[MAX_SIZE];
	int numberOfIntegers;

	ReadList(list, numberOfIntegers);

	PrintList(list, numberOfIntegers);

	return 0;
}
//------------------------------
void ReadList(int list[], int& length)
{
	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;

	while (number >= 0 && index < MAX_SIZE)
	{
		list[index] = number;
		index ++;
		cin >> number;
	}

	length = index;
}

//-----------------------------------

void PrintList(const int list[], int length)
{
	int index;

	cout << "The list contains " << length
		<< " nonnegative integers as follows:" << endl;

	for (index = length; index >= 0; index--)
		cout << list[index] << endl;
}
At first sight the only problem I see is that at line 50, you assign length to index, but your list[length] will have an uninitialized value in it.
Change line 50 to:
for (index = length - 1; index >= 0; index--)
If you still get errors, please say what numbers you're inputting.

Side note: In C++ you can declare the index variable within the for loop itself
1
2
for (int index = length - 1; index >= 0; index--)
    cout << list[index] << endl;

It's often clearer what's happening this way because it restricts the scope of the variable. /sidenote
Last edited on
now it works perfectly!!Thanks very very much bro!

But I don't get why index = length - 1would make such difference..

Sorry for being this stupid...
It's because arrays start at index 0 instead of index 1.

When you declare your int index[]; on line 13, your array becomes:
1
2
Element: [Junk][Junk][Junk][Junk][Junk]...[Junk][Junk][Junk]
Index:     0     1     2     3     4   ...  17    18    19

Your array hold 20 slots, BUT since the first slot is 0 instead of 1, that means that the last slot is 19 instead of 20. Also, the uninitialized slots still contain junk data in them.

Now your program runs and you fill in some values, and it updates the length variable:
1
2
Element: [ 11 ][ 23 ][ 54 ][Junk][Junk]...[Junk][Junk][Junk]
Index:     0     1     2     3     4   ...  17    18    19

and length becomes = 3.

When you try to access list[length] now, you are trying to access list[3].
list[3] means the 4th element of the list, which still contains junk (in your case, the junk happened to be -85693460)*. Hope that sheds some light.

*This is called undefined behavior, and could actually cause your program to become invalid or do weird stuff (but most likely either segfault/crash or give junk values).
Last edited on
thanks bro! I got it now
Topic archived. No new replies allowed.