Victor Martins Victor Martins - 5 months ago 37
C++ Question

The loops for this array works but I still get an Compiler Errorerror?

Hey guys so I just started working with arrays.

Could someone shed some light on this topic please. This was I created, I thought it made sense but I must be missing something.
I get an Debug Error Run-Time Check Failure #2 - S

#include <climits>

int lowestAmount(int[]);
int highestAmount(int[]);

using namespace std;

int main() {

const int AMOUNT = 9;

int values[AMOUNT];
int lowest, highest;

cout << "Please Insert 10 Numbers of your Choice: ";
cin >> values[0] >> values[1] >> values[2] >> values[3] >>
values[4] >> values[5] >> values[6] >> values[7] >>
values[8] >> values[9];
cout << endl;
lowest = lowestAmount(values);
highest = highestAmount(values);

cout << "/tThe Lowest out of all of them is: " << lowest <<
cout << "/tThe Highest out of all of them is: " << highest << endl ;

return 0;

int lowestAmount(int val[]) {

int lowest = INT_MAX;
int count = 10;
for (int i = 0; i < count; i++) {
if (val[i] < lowest)
lowest = val[i];

return lowest;

int highestAmount(int val[]) {
int highest = INT_MIN;
int count = 10;
for (int i = 0; i < count; i++) {
if (val[i] > highest)
highest = val[i];
return highest;

The code sort of works but not correctly all the time, and I can't figure out what I did wrong? I though I had the logic down??? Could someone shed some light???


Array indices start at zero, which means that the last index is the amount of elements minus one. You want the user to input 10 numbers, and you do read vales[0] up to values[9] but your array doesn't have the room for it.

const int AMOUNT = 9;

int values[AMOUNT];

That means you declare an array big enough for 9 elements, not 10. AMOUNT needs to be 10.

C++ does not automatically do bounds checking. This means that if you write outside of an array, you are going to get unpredictable behaviour, and you cannot even rely on the program always crashing.

There's also a bug in both your higher/lower functions that ignores the last element even as it stands now. Once you fix the array to be for 10 elements, you need to loop until i < 10 after that.

Also in the future, you will get better help by posting specifically what does not work, meaning you should show compiler / debugger errors, not just your code.