Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ STL Beginner
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 12-Apr-10 5:41am
Edited 5-May-10 23:10pm
Moak11.4K
v4
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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

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

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

(IMHO a good C/C++ tutorial would help)
Smile | :)
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are declaring minimum as a value a[0] before assigning a value to a[0].
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

very simple answer is to look at the loop and change > to <
i < arraysize
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

OMG Look at the accepted answer D'Oh! | :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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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 D'Oh! | :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;
 
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 6 May 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100