Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
XP XP is offline
external usenet poster
 
Posts: 389
Default Man in the middle

I am using Office 2003 on Windows XP.

In code, I need to work out which variable contains the middle value, in
this example, it would be 85. I would guess this needs to be done by
comparing <= and = and sometimes there could be a two or three way tie. In a
tie, it doesn't matter which one is in the middle...but the code needs to
identify which variable has been selected. So in the example, I would need to
know that dVar1 is in the
middle.

Dim dVar1 as Double
Dim dVar2 as Double
Dim dVar3 as Double
dVar1 = Abs(-85)
dVar2 = Abs(95)
dVar3 = Abs(25)

If dVar1 = dVar2 then ... (what next? I can't crack it!)

Do I have to try every combination to work this out? I can't seem to work
out the code to ensure that it always works. Your help is appreciated.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Man in the middle

I would advise you not to develop a specialized solution that only works for
the case of 3 items since you'll need to re-solve the problem when your boss
decides s/he wants to see the middle (wo)man of 5 items. The general
procedure is to sort your list and take the middle item from the list. A
simple explanation of bubble sort is:
1. Place your data in an array if N items
2. Create an outer loop that loops from N-1 to 1
3. Create in inner loop that loops from 0 to the value of the outer loop
variable (J).
4. In the inner loop compare item(J) to item (J+1) and swap if they are out
of order.
5. When the outer loop finishes the list is sorted. You can optimize it by
keeping track of whether there were any swaps in an iteration of the inner
loop and the first time there isn't one, your list is sorted.

There are quicker sorts but this is most suitable for a problem like yours.
I may have made some (hopefully) minor mistakes by regurgitating this from
memory but it's a good mental exercise to verify the logic.

Luke

"XP" wrote in message
...
I am using Office 2003 on Windows XP.

In code, I need to work out which variable contains the middle value, in
this example, it would be 85. I would guess this needs to be done by
comparing <= and = and sometimes there could be a two or three way tie.

In a
tie, it doesn't matter which one is in the middle...but the code needs to
identify which variable has been selected. So in the example, I would need

to
know that dVar1 is in the
middle.

Dim dVar1 as Double
Dim dVar2 as Double
Dim dVar3 as Double
dVar1 = Abs(-85)
dVar2 = Abs(95)
dVar3 = Abs(25)

If dVar1 = dVar2 then ... (what next? I can't crack it!)

Do I have to try every combination to work this out? I can't seem to work
out the code to ensure that it always works. Your help is appreciated.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Man in the middle

Sub MiddleValue()
Dim dVar1 As Double
Dim dVar2 As Double
Dim dVar3 As Double
Dim midval As Double
Dim s As String
dVar1 = Abs(-85)
dVar2 = Abs(95)
dVar3 = Abs(25)
midval = Application.Median(dVar1, dVar2, dVar3)
If dVar1 = midval Then s = "dVar1"
If dVar2 = midval Then s = "dVar2"
If dVar3 = midval Then s = "dVar3"
Debug.Print s, midval
End Sub

--
Regards,
Tom Ogilvy





"XP" wrote:

I am using Office 2003 on Windows XP.

In code, I need to work out which variable contains the middle value, in
this example, it would be 85. I would guess this needs to be done by
comparing <= and = and sometimes there could be a two or three way tie. In a
tie, it doesn't matter which one is in the middle...but the code needs to
identify which variable has been selected. So in the example, I would need to
know that dVar1 is in the
middle.

Dim dVar1 as Double
Dim dVar2 as Double
Dim dVar3 as Double
dVar1 = Abs(-85)
dVar2 = Abs(95)
dVar3 = Abs(25)

If dVar1 = dVar2 then ... (what next? I can't crack it!)

Do I have to try every combination to work this out? I can't seem to work
out the code to ensure that it always works. Your help is appreciated.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default Man in the middle

If the there are an odd number of variables, the Median function eliminates
all that for you. If it isn't an odd number, what would be the definition of
the middle value? <g

--
Regards,
Tom Ogilvy


"Luke Alcatel" wrote:

I would advise you not to develop a specialized solution that only works for
the case of 3 items since you'll need to re-solve the problem when your boss
decides s/he wants to see the middle (wo)man of 5 items. The general
procedure is to sort your list and take the middle item from the list. A
simple explanation of bubble sort is:
1. Place your data in an array if N items
2. Create an outer loop that loops from N-1 to 1
3. Create in inner loop that loops from 0 to the value of the outer loop
variable (J).
4. In the inner loop compare item(J) to item (J+1) and swap if they are out
of order.
5. When the outer loop finishes the list is sorted. You can optimize it by
keeping track of whether there were any swaps in an iteration of the inner
loop and the first time there isn't one, your list is sorted.

There are quicker sorts but this is most suitable for a problem like yours.
I may have made some (hopefully) minor mistakes by regurgitating this from
memory but it's a good mental exercise to verify the logic.

Luke

"XP" wrote in message
...
I am using Office 2003 on Windows XP.

In code, I need to work out which variable contains the middle value, in
this example, it would be 85. I would guess this needs to be done by
comparing <= and = and sometimes there could be a two or three way tie.

In a
tie, it doesn't matter which one is in the middle...but the code needs to
identify which variable has been selected. So in the example, I would need

to
know that dVar1 is in the
middle.

Dim dVar1 as Double
Dim dVar2 as Double
Dim dVar3 as Double
dVar1 = Abs(-85)
dVar2 = Abs(95)
dVar3 = Abs(25)

If dVar1 = dVar2 then ... (what next? I can't crack it!)

Do I have to try every combination to work this out? I can't seem to work
out the code to ensure that it always works. Your help is appreciated.




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Separate first name, middle name and last name NoviceUser Excel Worksheet Functions 1 December 18th 09 12:36 AM
Round up-down-or middle widman Excel Discussion (Misc queries) 4 April 27th 06 05:32 PM
how to get the middle name zomex Excel Worksheet Functions 2 December 6th 05 01:26 PM
how to get the middle name zomex Excel Worksheet Functions 1 December 5th 05 10:32 AM
Remove middle initial from "first name middle initial" Justin F. Excel Discussion (Misc queries) 15 September 26th 05 06:13 PM


All times are GMT +1. The time now is 07:04 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"