- Home /
Calculating the lowest number
I have a code where I'm getting values from scripts from objects in an array. I can get these values easily however, how can I compare them without having to do it manually since the array can get very long? What I need to do is compare each value to get the lowest value of them all, for example: I have 15, 20, 17, 81, 93, and 7. How can I get it to automatically find 7, I tried using Mathf.Min however I'll need a way to automatically enter in each value from each of the objects in the array with no persistent length?
EDIT: Okay what I've tried to do is join the contents of the array into a string separated by a comma and a space, however I can't compare this with Mathf.Min. How can I get this string to be usable in Mathf.Min?
Unless each object tracks its lowest value, your problem is always going to be O(n) complex (worst-case) where n = total number of array elements from all arrays.
Answer by fafase · Aug 03, 2012 at 09:48 AM
First off, this is just a variation in UnityScript of @ScroodgeM script. If your array is a declared as global (at the top) then you do not really need to pass it as it is accessible from anywhere in the script.
function MinValue(): float
{
var end:int=values.Length;
if (end <= 0)
return 0.0;
var minValue = values[0];
for(var i:int = 1; i < end; i++) //iterate through the array
{
if (minValue>values[i])minValue=values[i]; // If the minimum is greater then assign the value.
}
return minValue; //Finally return that value
}
Now if the array is not global (in case)
function MinValue(values : float[]): float
{
var end:int=values.Length;
if (end <= 0)
return 0.0;
var minValue = values[0];
for(var i:int = 1; i < end; i++)
{
if (minValue>values[i])minValue=values[i];
}
return minValue;
}
call it like
case 1:
var min : float = MinValue();
or case 2:
var min : float = MinValue(floatsArray);
EDIT:Updated the answer with the suggestions from @Eric5h5.
This is even better than using $$anonymous$$athf.$$anonymous$$in. I really like this function and i use it quite often, but in this case it's just slows it down. $$anonymous$$ath.$$anonymous$$in also does the if comparison, but regardless of the condition, $$anonymous$$Value is always reassigned each iteration.
Note: the behaviour of this function is a bit strange when you pass an empty array: It returns infinity.
I would do it like this, but it of course depends on what behaviour you expect:
// Unityscript
function $$anonymous$$inValue(values : float[]) : float
{
if (values.Length <= 0)
return 0.0;
var $$anonymous$$Value = values[0];
for(var i = 1; i < values.Length; i++)
{
if ($$anonymous$$Value > values[i])
$$anonymous$$Value = values[i];
}
return $$anonymous$$Value;
}
I guess you won't $$anonymous$$d if I update my answer with those relevant information. I just think of later consultations, I like when answer are updated so that I don't have to go all over the thread to play lego with the little pieces of updates.
For best speed you should use
var end = values.Length;
for (var i = 1; i < end; i++)
Also, technically .Length should be compared to an int (and it's not possible for it to be less than 0), and ideally should check for null, so:
if (values.Length <= 0.0)
I would write as
if (values == null || values.Length < 1)
Yes and yes ;) the 0.0 was a typo of course, but i like <= 0 more than < 1
Answer by ScroodgeM · Aug 02, 2012 at 07:56 PM
solution 1
write a simple method getting array returning min value, it will take O(n) time but will be easiest way to implementpseudocode
float MinValue(float[] values) { float minValue = Mathf.Infinity; for(int i = 0; i < values.Length; i++) { minValue = Mathf.Min(minValue, values[i]); } return minValue; }
to calc min value use:
float min = MinValue(floatsArray);
solution 2
write your simple array class with storing a min value while adding elements to it.Ok but how can I do the first solution as in I need pseudo code.
How can I get this to work in JS?
var $$anonymous$$inValue : float(fScore.cellScore : float[]){
How am I supposed to call this function, saying $$anonymous$$inValue(); isn't working?
Answer by Meltdown · Aug 03, 2012 at 09:45 AM
int[] iArr = { 5, 2, 3, 1, 4 };
Array.Sort(iArr);
Debug.Log(iArr[0]);
After sorting iArr[0] returns the lowest number.
Well, this is one of the shortest solutions, but also the most inefficient one ;) You sort the whole array just to get the lowest one. Afaik Array.Sort uses Quicksort (at least in Unity). So the complexity is on average O(n log n) or in the worst case O(n²)
For some reason I can't implement the $$anonymous$$value function. :/
Your answer
Follow this Question
Related Questions
C# - Problem with trigger that won't activate 1 Answer
C# Getting a certain AnimationState using strings from all objects with specific tag 2 Answers
JS: Array index not taking array.length as valid int? 2 Answers
Any way to 'GetComponent' as a base class? 2 Answers
Manipulate arrays in Inspector with Editor Script? 2 Answers