The lowest value

cla721

Member
Licensed User
Longtime User
I need an idea for a code to the following question:

A = 75
B = 1000
C = 25
D = 55

Wich one (A, B, C or D) is lowest...?

Best regards

Claus
 

mjcoon

Well-Known Member
Licensed User
I need an idea for a code to the following question:

A = 75
B = 1000
C = 25
D = 55

Wich one (A, B, C or D) is lowest...?

Best regards

Claus

Easy: answer is "C"!

B4X:
lowest = A
lowName = "A"
If lowest > B Then
lowest = B
lowName = "B"
End If

Repeat "If ..." for however many more variables...

HTH, Mike.
 

mjcoon

Well-Known Member
Licensed User
You could also use:
B4X:
lowest = Min(A, B)
lowest = Min(lowest, C)
lowest = Min(lowest, D)
Best regards.

That illustrates the need to specify the problem fully: do we merely have to determine the lowest value, or also which one (A|B|C|D) it was?

Mike.
 

warwound

Expert
Licensed User
Longtime User
If it's not required to find which variable has the lowest value - it is instead just the lowest value which is required then...

Add all values to a List and use the List Sort method.
That way you could easily get both the lowest value and the highest value if required.

Martin.
 

mjcoon

Well-Known Member
Licensed User
If it's not required to find which variable has the lowest value - it is instead just the lowest value which is required then...

Add all values to a List and use the List Sort method.
That way you could easily get both the lowest value and the highest value if required.

Martin.

Also true, but that illustrates the different hidden criteria behind coding. It would require writing more lines of code, the real intention would not be so obvious (because the sorted list is in fact not needed) and would take much more execution time (because sorting is not linear with the number of items being sorted).

On the other hand, if the list being sorted was of a value-key pair, the key could be used to tag each value with the identity of the originating variable. Thus the identity of the highest and lowest values would then be available...

Mike.
 
Top