Results 1 to 5 of 5

Thread: Finding the range of 4 numbers

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    4

    Finding the range of 4 numbers

    I'm probably just being stupid, but for some reason I can't figure out how to find the range (biggest number - smallest number) of four numbers. All of the numbers are stored in seperate variables, a, b, c and d. I can't find any Math or Integer methods that will do this.

    Again, the solution is probably very simple, but any help would be appreciated.
    Thanks!

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Finding the range of 4 numbers

    To find the range, first order the data from least to greatest. Then subtract the smallest value from the largest value in the set. Here is a quick example:
    Code:
    Dim data() As Integer = {25, 11, 1002, 1}
    data = data.OrderBy(Function(n) n).ToArray()
    Dim range As Integer = data.Last() - data.First()
    Edit - Or an old school example if you don't want to use LINQ:
    Code:
    Dim data() As Integer = {25, 11, 1002, 1}
    Array.Sort(data)
    Dim range As Integer = data(data.Length - 1) - data(0)
    Last edited by dday9; Feb 12th, 2016 at 01:04 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    4

    Re: Finding the range of 4 numbers

    Thanks for the reply! Just tried this, I get the error that OrderBy, Last and First are not part of arrays?

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: Finding the range of 4 numbers

    You would have to have the LINQ namespace referenced, which it should be by default if you're using the .NET framework 3.5 or higher. Otherwise if you cannot use LINQ take a look at my edit code.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    4

    Re: Finding the range of 4 numbers

    Ah, thanks! I'm using SharpDevelop, which musn't include LINQ by defualt. Ended up using the edit and it works perfectly! Thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width