•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,580 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,610 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 751 | Replies: 3 | Solved
![]() |
•
•
Join Date: Oct 2007
Posts: 33
Reputation:
Rep Power: 2
Solved Threads: 0
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;
}•
•
Join Date: Jul 2005
Posts: 1,288
Reputation:
Rep Power: 9
Solved Threads: 175
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.
•
•
Join Date: Oct 2007
Posts: 33
Reputation:
Rep Power: 2
Solved Threads: 0
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;
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- ambiguous part 3 (C)
- to find the execution time of a code (C)
- I want to know about the java code. Help please!!!! (Java)
- What is the problem with this line of my code? (Visual Basic 4 / 5 / 6)
- Pause Execution of a vb.net application (VB.NET)
- Execution time (C)
- CEdit boxes and mulitplication (C++)
- IE will not open new URL from address bar (Viruses, Spyware and other Nasties)
- Seek, and You Shall Find - vulnerabilities (Geeks' Lounge)
- WinXP and IE problems. Please Read. (Web Browsers)
Other Threads in the C++ Forum
- Previous Thread: Need help in my C++ Assignment
- Next Thread: string compare



Linear Mode