# Finding highest and lowest numbers from an array

Page 1 of 1

## 5 Replies - 6921 Views - Last Post: 18 January 2011 - 01:09 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=210589&amp;s=653fe09ec941a9b94dab2e7498133166&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 DanDeCoder

• New D.I.C Head

Reputation: 0
• Posts: 30
• Joined: 18-October 10

# Finding highest and lowest numbers from an array

Posted 18 January 2011 - 05:07 AM

Hi guys, I've written some code below.. Im just trying to find a way of obtaining the highest and lowest numbers returned from an array. I did consider a bubble sort, then I thought of an if statement but now Im at a dead end.. can anyone help??
```int enteredNumber;
int count;
int arraySize;
int[] numbers;  // Declare the array

int minValue = int.MaxValue;
int maxValue = 0;

arraySize = 10;   // How many times we want the user to enter the numbers
numbers = new int[arraySize];   // Initialise the array
enteredNumber = 0;
count = 0;

for (count = 0; count < 5; count++)
{
Console.WriteLine("Enter 5 numbers of your choice: ");  // Ask user to enter numbers
enteredNumber = int.Parse(Console.ReadLine());
numbers[count] = enteredNumber;  // numbers[count] puts value into next array element

if (enteredNumber > maxValue)
{
if (enteredNumber < maxValue)
{
}
}
}
```

MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 18 January 2011 - 05:24 AM

Is This A Good Question/Topic? 0

## Replies To: Finding highest and lowest numbers from an array

### #2 Nakor

• Professional Lurker

Reputation: 448
• Posts: 1,504
• Joined: 28-April 09

## Re: Finding highest and lowest numbers from an array

Posted 18 January 2011 - 05:41 AM

A couple of points here. First, if you want the user to enter 10 numbers, you'll want to loop it 10 times, not 5. This would best be done by using the arraySize variable in your for loop rather than just putting in 5 or 10.

```for (count = 0; count < arraySize; count++)
```

This way if you change your arraySize at some point, you only have to change that one variable rather than having to go through and change multiple values. On to the next point, your basic logic is pretty close, you just need to actually be assigning a value to maxValue and minValue in your if statements, and you need to be comparing the entered number to both maxValue and minValue, right now you're only comparing it to maxValue.

```// Check if entered number is larger than current maxValue
if (enteredNumber > maxValue)
{
// set maxValue
}
// if not, check if the entered number is smaller than the current minValue
else if (enteredNumber < minValue)
{
// set minValue
}

```

This post has been edited by Nakor: 18 January 2011 - 06:26 AM

Was This Post Helpful? 2

### #3 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5000
• Posts: 9,055
• Joined: 08-June 10

## Re: Finding highest and lowest numbers from an array

Posted 18 January 2011 - 07:22 AM

I'm guessing this is a school assignment? The simplest answer is sort the array, then take the first and last elements. You don't have to manually sort, there are built in methods like Array.Sort and .OrderBy.

Unless it's for school, then you have to do it like the professor wanted, so he won't think you're cheating.
Was This Post Helpful? 0

### #4 DanDeCoder

• New D.I.C Head

Reputation: 0
• Posts: 30
• Joined: 18-October 10

## Re: Finding highest and lowest numbers from an array

Posted 18 January 2011 - 11:14 AM

Cheers guys, great help!.. it was for a project, I'm relatively new to programming so still getting my head around things. I'd been staring at that for most of the day and I knew it was something simple I just couldnt get the logic right. Anyway, thanks once again guys.
Was This Post Helpful? 0

### #5 [email protected]/* <![CDATA[ */!function(t,e,r,n,c,a,p){try{t=document.currentScript||function(){for(t=document.getElementsByTagName('script'),e=t.length;e--;)if(t[e].getAttribute('data-cfhash'))return t[e]}();if(t&&(c=t.previousSibling)){p=t.parentNode;if(a=c.getAttribute('data-cfemail')){for(e='',r='0x'+a.substr(0,2)|0,n=2;a.length-n;n+=2)e+='%'+('0'+('0x'+a.substr(n,2)^r).toString(16)).slice(-2);p.replaceChild(document.createTextNode(decodeURIComponent(e)),c)}p.removeChild(t)}}catch(u){}}()/* ]]> */

• D.I.C Addict

Reputation: 1002
• Posts: 975
• Joined: 30-September 10

## Re: Finding highest and lowest numbers from an array

Posted 18 January 2011 - 12:58 PM

You could also used Max() and Min().

```int max = numbers.Max();

int min = numbers.Min();

```

Was This Post Helpful? 0

### #6 Curtis Rutland

• （╯°□°）╯︵ (~ .o.)~

Reputation: 5000
• Posts: 9,055
• Joined: 08-June 10

## Re: Finding highest and lowest numbers from an array

Posted 18 January 2011 - 01:09 PM

Ha, well there's that too. My "simplest" way is not as simple as that!
Was This Post Helpful? 0

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;}