|
-
Feb 12th, 2016, 12:46 PM
#1
Thread Starter
New Member
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!
-
Feb 12th, 2016, 12:51 PM
#2
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.
-
Feb 12th, 2016, 01:13 PM
#3
Thread Starter
New Member
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?
-
Feb 12th, 2016, 01:17 PM
#4
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.
-
Feb 12th, 2016, 01:46 PM
#5
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|