Jump to content
Sign in to follow this  
nullsystems

Finding smallest number in array?

Recommended Posts

Ello,

Question really quick...

How do you find the smallest number in an array?

_array = [a,b,c,d]

...told you it was quick lol.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

t1 = massy distance player

t2 = moddy distance player

t3 = eppy distance player

t4 = baggy distance player

_Array = [t1,t2,t3,t4];

_startNum = 900000;

_select = 0;

_find = 0;

_maxcount = count _Array;

#loop

_num = (_Array select _select)

?(_num < _startNum) : _Distance = _num;

_startNum = _num

_select = _select + 1

?_select < _maxcount: goto "loop"

I did have it working but I think I broke it, it always returns the last value in the array, not the smallest  confused_o.gif

Thank you !

Share this post


Link to post
Share on other sites

Aha! im a fool.

?(_num < _startNum) : _Distance = _num;

_startNum = _num

^ Should be:

?(_num < _startNum) : _Distance = _num; _startNum = _num

biggrin_o.gif There we go, quick lesson on finding the least distance to a player out of an array lol

Share this post


Link to post
Share on other sites

Just loop through the array with a while. Assign the first element of the array as the smallest variable and compare each following element with the currently smallest element and update the smallest-value variable if necessary:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_smallest = _array select 0;

_index = 0;

while(_index < count(_array))

{

if(_array select _index < _smallest)

{

_smallest = _array select _index;

};

};

That should be it.

Share this post


Link to post
Share on other sites

forEach will let you loop through an array very quickly.

Something like<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_smallest = _array select 0;

{if (_x < _smallest) {_smallest = _x;};} forEach _array;

Share this post


Link to post
Share on other sites

Funny how you can come up with so many solutions for one problem...

Turns out they all worked in the end, even mine lol, thanks for the info guys. smile_o.gif

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×