# Max and min value of array

Page 1 of 1

## 6 Replies - 9078 Views - Last Post: 03 May 2012 - 01:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=277779&amp;s=8ef76acc25adca1118eb8699ba3c766d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 loveandwar

Reputation: -2
• Posts: 14
• Joined: 08-April 12

# Max and min value of array

Posted 02 May 2012 - 09:22 AM

hi ,
how can i find maximum and minimum value of this array
```include<iostream>

using namespace std;
int division(int,int);
main()
{
int size=5;              //Array size
int array[size];          //Declaring array
int sum=0;
for(int i=0;i<size;i++)   //Loop which inputs arrays data and
//Calculates its sum
{
cout<<"Enter element number "<<i+1<<endl;
cin>>array[i];
sum=sum+array[i];
}
//Now calling division function to find the average...
cout<<"Average of array elements is "<<division(sum,size);

}
int division(int sum,int size)
{
return sum/size;
}
```

Is This A Good Question/Topic? 0

## Replies To: Max and min value of array

### #2 loveandwar

Reputation: -2
• Posts: 14
• Joined: 08-April 12

## Re: Max and min value of array

Posted 02 May 2012 - 09:36 AM

hi ,
how can i find the maximum and minimum value of this array

include<iostream>

```using namespace std;
int division(int,int);
main()
{
int size=5;              //Array size
int array[size];          //Declaring array
int sum=0;
for(int i=0;i<size;i++)   //Loop which inputs arrays data and
//Calculates its sum
{
cout<<"Enter element number "<<i+1<<endl;
cin>>array[i];
sum=sum+array[i];
}
//Now calling division function to find the average...
cout<<"Average of array elements is "<<division(sum,size);

}
int division(int sum,int size)
{
return sum/size;
}
```

### #3 sepp2k

• D.I.C Lover

Reputation: 2577
• Posts: 4,113
• Joined: 21-June 11

## Re: Max and min value of array

Posted 02 May 2012 - 09:40 AM

```int division(int sum,int size)
{
return sum/size;
}

```

If I may ask, what exactly was your motivation for writing that function?

### #4 loveandwar

Reputation: -2
• Posts: 14
• Joined: 08-April 12

## Re: Max and min value of array

Posted 02 May 2012 - 10:01 AM

sepp2k, on 02 May 2012 - 09:40 AM, said:

```int division(int sum,int size)
{
return sum/size;
}

```

If I may ask, what exactly was your motivation for writing that function?

Quote

all i concern my code is working or not , as it seems to me it is working but i don't know how can i find the maximum and minimum value from that array, i will appreciate if you will help rather than pointing my code .thanks

### #5 simeesta

Reputation: 221
• Posts: 594
• Joined: 04-August 09

## Re: Max and min value of array

Posted 02 May 2012 - 10:08 AM

Let the first element be min. Then iterate through the array, changing min if you find a smaller element.

### #6 aaa111

• D.I.C Regular

Reputation: 88
• Posts: 284
• Joined: 21-February 07

## Re: Max and min value of array

Posted 02 May 2012 - 11:15 AM

To find the maximum and minimum value,Initialize both max and min to the very first element of your array.Then compare both of the variable with rest of your array,if min < arr[i] then assign arr[i] to min,similarly you test the max just with reverse condition.

### #7 David W

• DIC supporter

Reputation: 298
• Posts: 1,839
• Joined: 20-September 08

## Re: Max and min value of array

Posted 03 May 2012 - 01:39 PM

Quote

how can i find maximum and minimum value of this array

You know ... 'sepp2k' raised a valid question ... Coding can be very concise ... i.e. many precise meanings are implied by 'saying' just a few 'words' ... and also many insights may be gleaned ... by the use/misuse of code
```include <iostream>

using namespace std;

double average( int[], int );

int main() // main returns an int ...
{
const int size = 5; // Array size; Note const int
int array[size]; // Get memory for 5 int's
for( int i = 0; i < size; ++i ) // Data input loop
{
cout << "Enter element number " << i+1 << ": " << flush;
cin >> array[i];
}

//Now calling function to find the average...
cout << "Average of array elements is: "<< average( array, size );
}

double average( int ary[], int size )
{
double sum = 0.0;
for( int i = 0; i <= size; ++i ) sum += ary[i];
return sum/size;
}
```

Hint: your functions to return max and min values will have similar prototypes ...
int max( int ary[], int size );
int min( int ary[], int size );

This post has been edited by David W: 03 May 2012 - 02:41 PM