1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Answered Smallest/lowest number from array

Discussion in 'Call of Duty: Black Ops 3 Answered Questions' started by Cxwh, Nov 30, 2016.

  1. Cxwh

    Cxwh Active Member

    54
    33
    18
    Credits:
    1,180
    How do I get the smallest number of an array? In a "clean" way? Maybe use arraySort idk...
     
  2. thahitcrew

    thahitcrew Modder

    224
    261
    163
    Credits:
    270
    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: Dec 1, 2016
    LowKeyDNS, CabCon, Cxwh and 1 other person like this.
  3. CabCon

    CabCon Head Administrator Staff Member

    3,307
    2,003
    263
    Credits:
    73,356
    @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
     
  4. LowKeyDNS

    LowKeyDNS Guest

    Credits:
    0
    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!
     
    SCP and CabCon like this.
  5. thahitcrew

    thahitcrew Modder

    224
    261
    163
    Credits:
    270
    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
     
    LowKeyDNS likes this.
  6. CabCon

    CabCon Head Administrator Staff Member

    3,307
    2,003
    263
    Credits:
    73,356
    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:
     
  7. LowKeyDNS

    LowKeyDNS Guest

    Credits:
    0
    I didn't mean it that way at all :grinning:. There are always several ways to solve a problem, and I just wanted to help out!
     
    CabCon and thahitcrew like this.

Share This Page