Answered Smallest/lowest number from array

Cxwh

Veteran
Messages
64
Reaction score
45
Points
793
How do I get the smallest number of an array? In a "clean" way? Maybe use arraySort idk...
 

thahitcrew

Modder
Messages
219
Reaction score
211
Points
163
here's something you could do without the use of any 'helper functions'
Code:
int smallestNum = 99999999;
const int arrayElements = 5;
int idkExampleArray[arrayElements] = { 122, 8, 24, 6, 65 };
for(int i = 0; i < arrayElements; i++)
{
    if(idkExampleArray[i] < smallestNum) smallestNum = idkExampleArray[i];
}
//smallest number will be stored in smallestNum when for loop finishes looping..
//e.g. in this case smallestNum = 6
this would work fine. If you're doing it in GSC you just need to change a few things and it should work fine.
 
Last edited:

CabCon

Head Administrator
Staff member
Head Staff Team
Messages
4,998
Reaction score
2,918
Points
1,053
How do I get the smallest number of an array? In a "clean" way? Maybe use arraySort idk...
@thahitcrew described how to get the lowest number very good.

With this function you can sort an array by their value:
Code:
function func_sort_by_value( &array, b_lowest_first = false )
{
    return merge_sort( array, &func_sort_by_value_compare_func, b_lowest_first );
}

function func_sort_by_value_compare_func( val1, val2, b_lowest_first )
{
    if ( b_lowest_first )
    {
        return val1 < val2;
    }
    else
    {
        return val1 > val2;
    }
}

Be sure to include array_shared.gsc
 
L

LowKeyDNS

Guest
How do I get the smallest number of an array? In a "clean" way? Maybe use arraySort idk...
CabCon's solution will work, however, there is a much faster sort for larger arrays called a quicksort, which uses ONlogN as in comparison to the merge sort, ON^2. You can find this function in the array_shared.gsc as well, and code for implementation is as follows:

Code:
#using scripts/shared/array_shared;

function SortWrapper( array )
{
   return array::quickSort( array ); //You may optionally define your own comparison function
}

function GetLowestBySort( array )
{
  return SortWrapper( array )[ 0 ]; //Get the first element in the array, which is our lowest
}

Hope this helps!
 

thahitcrew

Modder
Messages
219
Reaction score
211
Points
163
CabCon's solution will work
Mine does too lmao. You're saying that as if my solution will not work. Like i mentioned my method of getting lowest number doesn't make use of any helper functions and/or may not be the fastest, but it works 100% completly fine
 

CabCon

Head Administrator
Staff member
Head Staff Team
Messages
4,998
Reaction score
2,918
Points
1,053
CabCon's solution will work, however, there is a much faster sort for larger arrays called a quicksort, which uses ONlogN as in comparison to the merge sort, ON^2. You can find this function in the array_shared.gsc as well, and code for implementation is as follows:

Code:
#using scripts/shared/array_shared;

function SortWrapper( array )
{
   return array::quickSort( array ); //You may optionally define your own comparison function
}

function GetLowestBySort( array )
{
  return SortWrapper( array )[ 0 ]; //Get the first element in the array, which is our lowest
}

Hope this helps!
That's a very elegant solution! Top answer. I don't know this way, thank you! Good job! :y:

Also @thahitcrew way, is working also for sure! :grinning:
 
L

LowKeyDNS

Guest
Mine does too lmao. You're saying that as if my solution will not work. Like i mentioned my method of getting lowest number doesn't make use of any helper functions and/or may not be the fastest, but it works 100% completly fine
I didn't mean it that way at all :smile:. There are always several ways to solve a problem, and I just wanted to help out!
 
Top