User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Oct 2007
Posts: 33
Reputation: rogenie is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rogenie rogenie is offline Offline
Light Poster

Why is my code execution truncated....

  #1  
Nov 3rd, 2007
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;
	
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2005
Posts: 1,288
Reputation: Lerner is just really nice Lerner is just really nice Lerner is just really nice Lerner is just really nice 
Rep Power: 9
Solved Threads: 175
Lerner Lerner is offline Offline
Nearly a Posting Virtuoso

Re: Why is my code execution truncated....

  #2  
Nov 4th, 2007
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.
Reply With Quote  
Join Date: Oct 2007
Posts: 33
Reputation: rogenie is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rogenie rogenie is offline Offline
Light Poster

Re: Why is my code execution truncated....

  #3  
Nov 4th, 2007
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;

}
Reply With Quote  
Join Date: Oct 2007
Posts: 33
Reputation: rogenie is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rogenie rogenie is offline Offline
Light Poster

Re: Why is my code execution truncated....

  #4  
Nov 4th, 2007
Okay never mind I figured it out. thanks....
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:22 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC