Here, there are a couple of problems:
1 2 3 4 5 6
|
for (int i = 0; i < 10; i +1)
{
cout << "Enter number" << i++ << " ";
cin >> listnum[i];
minMax(number);
}
|
When it is run, the output is:
Notice that the index printed is zero, which is what one would expect. The
i++
increments the value after accessing its current value.
So when the execution gets to the next line,
the value of
i
is now 1 (one). So the user input is stored in the second element of the array. Remember arrays are indexed starting from zero. So the first element
listnum[0]
is not used. More of a problem, what happents to the tenth number input by the user? It ought to go in
listnum[9]
but instead goes into
listnum[10]
. That's a serious problem, it is outside the array, so will corrupt some other area of memory with unpredictable results.
I guess what you wanted to do was to display to the user the values from 1 to 10, since that's the way humans count, but at the same time the computer needs to count from 0 to 9.
Here's one way to do it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
const int size = 10;
int listnum[size];
for (int i = 0; i < size; i++)
{
cout << "Enter number " << i+1 << " ";
cin >> listnum[i];
}
cout << "\n\nContents of array\n";
for (int i = 0; i < size; ++i)
{
cout << listnum[i] << ' ';
}
cout << '\n';
|
I added the next part to display the array as an aid to checking that everything has worked so far.
Haven't yet go to the function minMax(). That's next, now the start of the program is debugged.