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
@thahitcrew described how to get the lowest number very good.How do I get the smallest number of an array? In a "clean" way? Maybe use arraySort idk...
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;
}
}
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:How do I get the smallest number of an array? In a "clean" way? Maybe use arraySort idk...
#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
}
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 fineCabCon's solution will work
That's a very elegant solution! Top answer. I don't know this way, thank you! Good job!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!
I didn't mean it that way at all . There are always several ways to solve a problem, and I just wanted to help out!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