Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the max and min from an array?

Status
Not open for further replies.

queenie680

Programmer
Apr 16, 2002
4
US
Hi,

Does anyone know how to get the largest and smallest number from an array?

float salary[100];

then I have a for loop here that loads the array....now how do you get the largest and smallest number in the array? Thank you for your time =)

Serena
 
Loop through it with a variable max initialized to zero and if the current value > max, set min to the value.

Second, with an unsigned int set to negative 1 loop through and set it if it is greater then the current value.

negative 1 in and unsigned in is the max value of an int

Matt
 
This looks a little bit complicated !
Here is some code that does the same thing.

#include <iostream.h>
#define _MAXSIZE_ 100
void main()
{
float max;
float min;
float array[_MAXSIZE_];
int size;
cout<<&quot;This program can determin the maximun and minimum &quot;<<endl;
cout<<&quot;of a sequence of 100 numbers or less.&quot;<<endl;
cout<<&quot;How long will be your sequence ?&quot;<<endl;
cin>>size;
if( size > _MAXSIZE_ )
cout<<&quot;This sequence is too big !&quot;<<endl;
else
{
for( int i = 0; i < size; i++ )
{
cout<<&quot;x&quot;<<i + 1<<&quot; = &quot;;
cin>>array;
}
}
min = array[0];
for( int j = 0; j < size; j++ )
{
if( array[j] < min )
min = array[j];
}
max = array[0];
for(j = 0; j < size; j++ )
{
if( array[j] > max )
max = array[j];
}
cout<<&quot;The maximum is: &quot;<<max<<endl;
cout<<&quot;The minimum is: &quot;<<min<<endl;
}
 
Thank you so much for your help I really appreciate it!
 
You are welcome !
Here is some little corrections to the original code.

#include <iostream.h>
#include <stdlib.h>
#define _MAXSIZE_ 100
void main()
{
float max;
float min;
float array[_MAXSIZE_];
int size;
cout<<&quot;This program can determin the maximun and minimum &quot;<<endl;
cout<<&quot;of a sequence of 100 numbers or less.&quot;<<endl;
cout<<&quot;How long will be your sequence ?&quot;<<endl;
cin>>size;
if( size > _MAXSIZE_ || size <= 0 )
{
if( size > _MAXSIZE_ )
cout<<&quot;This sequence is too big !&quot;<<endl;
else if( size <= 0 )
cout<<&quot;The size cant be a negative number.&quot;<<endl;
exit(0);
}
// gets the sequence elements for the user
for( int i = 0; i < size; i++ )
{
cout<<&quot;x&quot;<<i + 1<<&quot; = &quot;;
cin>>array;
}
//determin the minimum
min = array[0];
for( int j = 0; j < size; j++ )
{
if( array[j] < min )
min = array[j];
}
//determin the maximum
max = array[0];
for(j = 0; j < size; j++ )
{
if( array[j] > max )
max = array[j];
}
cout<<&quot;The maximum is: &quot;<<max<<endl;
cout<<&quot;The minimum is: &quot;<<min<<endl;
}

 
If you wanna get funky you can streamline the above with funcitons as follows

int max(int x, int y)
{
return (x>y ? x:y);
}

int min(int x, int y)
{
return (x<y ? x:y);
}

and in a for loop do

Code:
int minVal = array[0];
maxVal = array[0];
for(int i = 1;i<SIZE_OF_ARRAY;i++)
{
   maxVal = max(maxVal,array[i]);
   minVal = min(minVal,array[i]);
}


Or you could just put the one line of code in the functions into the for loop. When the loop is done, you have the max and min values

Matt

 
Hi,

I'm a new user to this forum, Would appreciate if anyone can help me in this:

I need to find the index of the minimum value in a float array: float vector[index]

Can anyone tell me how I can do that?

Thanks alot!!
 
I think this is almost same as i told here yesterday..
Hope it helps..

#include &quot;stdafx.h&quot;
#include &quot;stdlib.h&quot;
#include &quot;conio.h&quot;
#include <stdio.h>

int main(int argc, char* argv[])
{
int i, j;
double a[4],ai;

a[0] = 9.3; /* a sentinel */
a[1] = 4.2; /* a sentinel */
a[2] = 7.6; /* a sentinel */
a[3] = 3.3; /* a sentinel */


for(i=0; i <4; i++)
{ /* invariant: a[1..i-1] sorted */
ai = a;
j = i-1;
while( a[j] > ai )
{ /* invariant: a[j+2..i] > ai */
a[j+1] = a[j];
j--;
}
/* a[1..j] <= ai < a[j+2..i] */

a[j+1] = ai;
/* a[1..i] is sorted */
}
for(int k=0;k<4;k++)
printf(&quot;\n%1.1f&quot;,a[k]);


printf(&quot;\nMin is=%1.1f&quot;,a[0]);
printf(&quot;\nMax is=%1.1f&quot;,a[3]);
return 0;
}
 
While it's good for beginners to see how this code works, I feel compelled to point out that there's a much simpler and faster way to get the same result:

[tt]const int SIZE = 10;
int array[ SIZE ];

// fill array somehow...

cout << &quot;Minimum: &quot; << min_element( array, array + SIZE );
cout << &quot;Maximum: &quot; << max_element( array, array + SIZE );
[/tt]

min_element and max_element are in <algorithm>, in the std namespace.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top