Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
What is the problem in this code?

#include
using namespace std;

int main ()

{	int const arraysize=10;
	int a[arraysize];
	int maximum=0;
	int minimum=a[0];
	int i;
	
	for ( i= 0 , i > (arraysize) , i++ )
	{
		cin>>a[i];
		
		if (a[i]>=maximum)
			maximum=a[i];

		if(a[i]<=minimum)
			minimum=a[i];
		
	}
	
		cout<<"the maximum of array a is : "<<maximum<<endl;
		cout<<"the minimum of array a is : "<<minimum<<endl;
 
		
return 0;

}
Posted
Updated 5-May-10 22:10pm
v4

joek1991 wrote:
int maximum=0; int minimum=a[0];

change to
C++
int maximum = INT_MIN;
int minimum = INT_MAX;



joek1991 wrote:
for ( i= 0 , i > (arraysize) , i++ )

change to
C++
for ( i = 0 ; i < arraysize ; i++ )



(IMHO a good C/C++ tutorial would help)
:)
 
Share this answer
 
IMO, you would have pinpointed this problem in 1 minute, by using the debugger and single-step your program.
Learning how to use the debugger is well worth the time spent.
 
Share this answer
 
You are declaring minimum as a value a[0] before assigning a value to a[0].
 
Share this answer
 
hi,

first initialize the value of a[0]

a[0]=0;

second

for ( i= 0 ;i < (arraysize);i++ )

hope now u can run ur programme.
 
Share this answer
 
I think so. Loop //for ( i= 0 , i > (arraysize) , i++ ) not has a end point. in fact, this willn't be excuted because conditions is not true
 
Share this answer
 
very simple answer is to look at the loop and change > to <
i < arraysize
 
Share this answer
 
OMG Look at the accepted answer :doh:
He didnt event point the actual problem in for loop check expression. and there is nothing wrong with setting junk/default value to minimum as it will get updated in loop.
 
Share this answer
 
Y u ppl recommending to hardcore values of minimum and maximum to 0 and let it use as it is for checks?
Think what if minimum value set to 0 and actual minimum value in array is 1 :doh:

your actual code should be like this

#include
using namespace std;

int main ()

{	int const arraysize=10;
	int a[arraysize];
	int maximum=0;  
	int minimum=0;  
	int i;
	
	for ( i= 0 , i > (arraysize) , i++ )
	{
		cin>>a[i];

		if (a[i]>=maximum || i==0)
			maximum=a[i];

		if(a[i]<=minimum || i==0)
			minimum=a[i];
	}
	
		cout<<"the maximum of array a is : "<<maximum<<endl;
		cout<<"the minimum of array a is : "<<minimum<<endl;
 
		
return 0;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900