8 Replies - 2509 Views - Last Post: 13 July 2010 - 12:43 AM Rate Topic: -----

#1 fariba_yoo   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 89
  • Joined: 17-May 08

Array Max and Min

Posted 12 July 2010 - 09:04 AM

View Postsyinamdar, on 01 February 2007 - 10:46 PM, said:

// Find maximum and minimum element in an arary.
#include<stdio.h>
int
main ()
{
  int i;
  int a[10] = { 10, 55, 9, 4, 234, 20, 30, 40, 22, 34 };
  int max = a[0];
  int min = a[0];

  for (i = 0; i < 10; i++)
	{
	  if (a[i] > max)
		{
		  max = a[i];
		}
	  else if (a[i] < min)
		{
		  min = a[i];
		}
	}
  printf ("Maximum element in an array : %d\n", max);
  printf ("Minimum element in an array : %d\n", min);

  return 0;
}



But how if I want to get numbers from user, like this:

int a[10];
int main()
{
	int Min=0;
	cout<<"Enter 10 number to find Minimum number: "<<endl;
	for (int i=0; i<10; i++)
	{
		cin>>a[i];
		if (Min>a[i])
		{
			Min=a[i];
		}
	}
	cout<<"Min is: "<<Min;
	getch();
}


here I have problem, I don't know how to initialize Min?

Is This A Good Question/Topic? 0
  • +

Replies To: Array Max and Min

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Array Max and Min

Posted 12 July 2010 - 09:07 AM

Code tags added and split from old topic.

As for your question, you could fill the array with data first then iterate through checking min's value against each index.

'min' is already initialized to zero when it is declared.
Was This Post Helpful? 0
  • +
  • -

#3 tauit_dnmd   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 129
  • Joined: 25-March 10

Re: Array Max and Min

Posted 12 July 2010 - 09:24 AM

View Postfariba_yoo, on 12 July 2010 - 08:04 AM, said:

View Postsyinamdar, on 01 February 2007 - 10:46 PM, said:

// Find maximum and minimum element in an arary.
#include<stdio.h>
int
main ()
{
  int i;
  int a[10] = { 10, 55, 9, 4, 234, 20, 30, 40, 22, 34 };
  int max = a[0];
  int min = a[0];

  for (i = 0; i < 10; i++)
	{
	  if (a[i] > max)
		{
		  max = a[i];
		}
	  else if (a[i] < min)
		{
		  min = a[i];
		}
	}
  printf ("Maximum element in an array : %d\n", max);
  printf ("Minimum element in an array : %d\n", min);

  return 0;
}



But how if I want to get numbers from user, like this:

int a[10];
int main()
{
	int Min=0;
	cout<<"Enter 10 number to find Minimum number: "<<endl;
	for (int i=0; i<10; i++)
	{
		cin>>a[i];
		if (Min>a[i])
		{
			Min=a[i];
		}
	}
	cout<<"Min is: "<<Min;
	getch();
}


here I have problem, I don't know how to initialize Min?


Because you init for Min=0, so if you input a number (>0) ,it always return 0.
To solve you can you a variable BOOL to check and init value for Min
Such as: bool first;
Here.
#include<iostream>
using namespace std;

int a[10];
int main()
{
	bool first=true; // input first time.
	int Min=0;
	cout<<"Enter 10 number to find Minimum number: "<<endl;
	for (int i=0; i<10; i++)
	{
		cin>>a[i];
		if(first)
		{
			first=false; 
			Min=a[i]; //init for Min in the first time .
		}
		else if(Min>a[i]) Min=a[i];
	}
	cout<<"Min is: "<<Min;
	return 0; //you must return a value .
}




This post has been edited by tauit_dnmd: 12 July 2010 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: Array Max and Min

Posted 12 July 2010 - 09:50 AM

No need to use a boolean:
#include<iostream>
using namespace std;

int a[10];
int main()
{
	int Min=0;
	cout<<"Enter 10 number to find Minimum number: "<<endl;
	for (int i=0; i<10; i++)
	{
		cin>>a[i];
		if( !i || Min>a[i] )
		{
			Min=a[i];
		}
	}
	cout<<"Min is: "<<Min;
	return 0; //you must return a value .
}


;)

This post has been edited by CTphpnwb: 12 July 2010 - 09:51 AM

Was This Post Helpful? 2
  • +
  • -

#5 fariba_yoo   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 89
  • Joined: 17-May 08

Re: Array Max and Min

Posted 12 July 2010 - 11:08 AM

View PostCTphpnwb, on 12 July 2010 - 08:50 AM, said:

No need to use a boolean:
#include<iostream>
using namespace std;

int a[10];
int main()
{
	int Min=0;
	cout<<"Enter 10 number to find Minimum number: "<<endl;
	for (int i=0; i<10; i++)
	{
		cin>>a[i];
		if( !i || Min>a[i] )
		{
			Min=a[i];
		}
	}
	cout<<"Min is: "<<Min;
	return 0; //you must return a value .
}


;)


Thanks a lot, it worked, can you please explain about this part: if( !i || Min>a[i] )
I couldn't understand !i part....
Was This Post Helpful? 0
  • +
  • -

#6 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: Array Max and Min

Posted 12 July 2010 - 11:11 AM

'i' is an integer. '!' is the logical NOT

0 and 1 can be equated as false/true

Thus NOT zero = true and NOT [any other number] = false
Was This Post Helpful? 2
  • +
  • -

#7 OnlineCop   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 73
  • Joined: 16-April 10

Re: Array Max and Min

Posted 12 July 2010 - 08:14 PM

Can you use negative numbers?

If negatives are NOT allowed, it may be easier to do this:
int main()
{
  unsigned int Min = (unsigned int) -1; // Basically, a VERY LARGE positive number

  ... // all the rest of your code
}


What this does is that the user will enter a number. Obviously, it is smaller than "the largest possible integer value" (which the (unsigned int) -1 is set to), so therefore, the "new" value becomes the new Min.

Now, if negatives ARE valid, you could set it to the largest positive number available (in this case, you can change it to the hex value 0x7FFFFFFF). It will still be greater than whatever value they enter, and therefore, you're still in the clear.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_guest*


Reputation:

Re: Array Max and Min

Posted 12 July 2010 - 10:19 PM

View PostCTphpnwb, on 12 July 2010 - 08:50 AM, said:

No need to use a boolean:
#include<iostream>
using namespace std;

int a[10];
int main()
{
	int Min=0;
	cout<<"Enter 10 number to find Minimum number: "<<endl;
	for (int i=0; i<10; i++)
	{
		cin>>a[i];
		if( !i || Min>a[i] )
		{
			Min=a[i];
		}
	}
	cout<<"Min is: "<<Min;
	return 0; //you must return a value .
}


;)


Nice short code ...

This is a little longer but may be easier for the OP to see the logic in each loop ... (that only the first time through the loop are min/max set to the value at index 0) ...

#include <iostream>
using namespace std;

const int ARY_SIZE = 10;

int main()
{
	cout << "Enter " << ARY_SIZE << " numbers to find minimum number ...\n\n";
	
    int a[ARY_SIZE], max, min;
	for( int i = 0; i < ARY_SIZE;  ) // note: index 'i' is updated iff good data
	{
        cout << "Number " << i+1 << " : " << flush;
		cin >> a[i];
		
		if( !cin.good() )
		{
            cin.clear(); // clear error flags
            cin.sync(); // 'flush' cin stream
            cout << "Integers only please ... ";
            continue; // go to top of 'for loop' right now ...
        }
        
		if( i == 0 ) max = min = a[0]; // executed only first time through loop ...
		else if( a[i] < min ) min = a[i]; // update min only if required
		else if( a[i] > max ) max = a[i];
		
		++ i;
	}
	
	cout << "\nMinimum is " << min << " and maximum is " << max;

    cout << "\n\nPress 'Enter' to continue/exit ... " << flush;
    cin.sync(); 
    cin.get(); // keep 'Window' open until 'Enter' key is pressed ...

    // Note: the C++ compiler will supply a 'return 0;' for you ...
}


Was This Post Helpful? 1

#9 fariba_yoo   User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 89
  • Joined: 17-May 08

Re: Array Max and Min

Posted 13 July 2010 - 12:43 AM

Thanks, it was useful :rolleyes2:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1