Page 1 of 1

## Finding Min, and Max numbers in an Array Rate 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=206002&amp;s=057e79851436c6961b7741ab0dde3f6c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Scottm

Reputation: 42
• Posts: 238
• Joined: 11-February 10

Posted 18 December 2010 - 03:41 PM

I've seen many posts lately, where people are asking on how to find the Max and Min numbers in an Array. So, here is a tutorial to find them::

Things you should previously know::

Some C++ knowledge, along with some Array knowledge

Let's get into it.

Finding max value::

First, make your array. In this tutorial I will use "Array" as the name for it.
```int Array[] = {1,2,3,4,5};
```

The numbers in the brackets {}, are the values stored in Array[]. The number 1 is stored in Array[0], the number 2 is stored in Array[1], and so on.

Now, make an int, and set it equal to 0
```int maxValue = 0;
```

This int will store the value of the highest number in the array.
Make a for loop::
```for(int i = 0; i < 5; i++){
```

The purpose of the for loop, is to take the index i and put it in Array[]. Thus making it Array[i]. And since values are stored into Array[0],Array[1]... You can call those values easier by using Array[i], and having i++ every time it is less than 5.

```if(Array[i] > maxValue){
maxValue = Array[i];
}
```

What this if statement is saying, is that if the value stored in Array[i] is greater than maxValue (it is 0 the first time through the for loop) it will store the value in Array[i] as maxValue. Now it will do this 5 times, and every time Array[i] is greater than maxValue, it will store that value in it.

So, with the values in our Array, it will go through and see if 1 is greater than 0. Since it is, it will store 1 into maxValue. Now it will go through the program with Array[i] as 2. Since 2 is greater than 1, it will store it into maxValue. It will continue through this 5 times, leaving the value 5 as maxValue, and that is our highest number!

```}
cout << "The highest value is: " << maxValue;
```

What I did there, was close the for loop, so it only prints the highest value once, then it prints it out.

And that's it to find the max number!

*************************************************

Finding min value::
Since finding min value is almost the same, just backwards, I will go through this a little faster

```int Array[] = {1,2,3,4,5};
int lowestNum = Array[0];
```

I assigned lowestNum to the value in Array[0], so I can compare the other values to it
```for(int i = 1; i < 5; i++){
```

Notice I started the int i at 1 this time. Rather than 0. I did this because I want to start at spot Array[1], so I can compare Array[1] to Array[0] (Which is lowestNum)
```if(Array[i] < lowestNum){
lowestNum = Array[i];
}
}
cout << "The lowest number is: " << lowestNum;
```

Okay, now in the if statement, I have it where if the value stored in Array[i] is less than lowestNum, it will store the value if Array[i] into lowestNum, so it can be compared to the next Array value. I close the if statement and for loop, the print out the value.

And there you go, if you have any questions, feel free to post or send me a message!

Is This A Good Question/Topic? 3

## Replies To: Finding Min, and Max numbers in an Array

### #2 Guest_Anon*

Reputation:

Posted 21 December 2010 - 03:23 AM

I believe it's worth pointing out the proper way to do this when you aren't doing homework.

http://www.sgi.com/t...ax_element.html
http://www.sgi.com/t...in_element.html

### #3 Kutlar

• New D.I.C Head

Reputation: 6
• Posts: 29
• Joined: 04-March 10

Posted 21 December 2010 - 04:58 PM

This can also be done in one line of code using the algorithm library.

### #4 David W

• DIC supporter

Reputation: 281
• Posts: 1,791
• Joined: 20-September 08

Posted 06 February 2011 - 07:23 PM

Your method for min was fine.
Your method for max will only work, for arrays having some value greater than 0.
It would work for all arrays if you used the same method as in your method for min, i.e. Set the max value to array[0] to begin, then compare with rest of array and update max as appropriate. For those who are using C, there are some snippets you may like to see. I'll try to find the links and update ...

This link is for C users and also features a C emulation of the C++ vector container ...

http://www.dreaminco...snippet2613.htm

This post has been edited by David W: 06 February 2011 - 07:30 PM

### #5 leafdj

• New D.I.C Head

Reputation: 3
• Posts: 43
• Joined: 05-February 11

Posted 11 February 2011 - 04:10 AM

David W, on 06 February 2011 - 07:23 PM, said:

Your method for min was fine.
Your method for max will only work, for arrays having some value greater than 0.
It would work for all arrays if you used the same method as in your method for min, i.e. Set the max value to array[0] to begin, then compare with rest of array and update max as appropriate. For those who are using C, there are some snippets you may like to see. I'll try to find the links and update ...

This link is for C users and also features a C emulation of the C++ vector container ...

http://www.dreaminco...snippet2613.htm

Thanks Scott for the post, it was a good introduction into using arrays, and the method you used, even if it wasn't the most efficient, was good practice for visualizing for loops.

That being said, David was right with the above suggestion.
Combining your inputs made a neat little addition to a simple calculator program I wrote.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }