5 Replies - 4844 Views - Last Post: 18 January 2011 - 01:09 PM Rate Topic: -----

#1 DanDeCoder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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!!!

:code:

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  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • 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  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,710
  • 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  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • 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  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4437
  • View blog
  • Posts: 7,710
  • 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