Why does the code below stop executing after displaying all the numbers stored in an array?

//this will let the user enter 10 values and
//store them into an array. After that
//sort it out and cout the largest and the smallest number entered

#include <iostream>
using namespace std;

int main()
{
const int NUM_ARRAY = 10; //number of numbers to be entered
int numHolder[NUM_ARRAY]; // where the numbers will be stored
int count; //accumulator
int highest; //highest number
int lowest; //lowest number

//input the numbers
for (count = 0; count < NUM_ARRAY; count++)
{
cout << "Please enter series of numbers"
<< (count + 1) << ": ";
cin >> numHolder[count];
}
//show the numbers entered
cout <<"The numbers you entered are: \n";
for (count = 0; count < NUM_ARRAY; count++)
cout << numHolder[count]<< endl;

//pick the highest number
cout << "The highest is: ";
highest = numHolder[0];
for (int hcount = 0; hcount < numHolder[count]; hcount++)
{
if (numHolder[count] > highest)
highest = numHolder[count];
}

return 0;

}

## All 3 Replies

At the end of the second for loop count equals NUM_ARRAY. count isn't changed between then and it's use in the terminating condition in the third for loop. This means that in the third for loop numHolder[count] is the same as numHolder[NUM_ARRAY] which is out of bounds. The same argument holds for the conditional of the if statement in the third for loop. The third for loop has even bigger problems than that though. Try changing the information inside the () of the third for loop to be the same as they are in the other two and run that.

Okay I made changes. Here is my code but the ouput is wrong. Hmmmmmm....

//this will let the user enter 10 values and
//store them into an array. After that
//sort it out and cout the largest and the smallest number entered

#include <iostream>
using namespace std;

int main()
{
const int NUM_ARRAY = 10; //number of numbers to be entered
int numHolder[NUM_ARRAY]; // where the numbers will be stored
int count; //accumulator
int highest; //highest number
int lowest; //lowest number

//input the numbers
for (count = 0; count < NUM_ARRAY; count++)
{
cout << "Please enter series of numbers"
<< (count + 1) << ": ";
cin >> numHolder[count];
}
//show the numbers entered
cout <<"The numbers you entered are: \n";
for (count = 0; count < NUM_ARRAY; count++)
cout << numHolder[count]<< endl;

for (int count = 1; count < NUM_ARRAY; count++)
{
highest = numHolder[0];
if (numHolder[count] > highest)
highest = numHolder[count];
}

//pick the highest number
cout << "The highest is: " << highest << endl;

return 0;

}

Okay never mind I figured it out. thanks....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.