none
Smallest number in an array RRS feed

  • Question

  • How do I find the smallest number in an array of numbers? I couldn't find any examples in small basic. I tried to look at examples in other programming languages, but I couldn't find a pattern. I thought maybe I could use bubble sort but realized I just need to get the smallest number regardless of the order of the numbers. Thanks in advance.
    Thursday, July 10, 2014 5:46 PM

Answers

  • Or without extensions, check each array value:

    Arr[1]=5
    Arr[2]=8
    Arr[3]=7
    Arr[4]=2
    Arr[5]=4
    minArr = Arr[1]
    For i = 2 To Array.GetItemCount(Arr)
      minArr = Math.Min(minArr,Arr[i])
    EndFor
    TextWindow.WriteLine("Minimum is "+minArr)
    • Marked as answer by Ezra94 Friday, July 11, 2014 1:21 AM
    Thursday, July 10, 2014 8:20 PM
    Moderator
  • Or again with LitDev extension:

    Arr[1]=5
    Arr[2]=8
    Arr[3]=7
    Arr[4]=2
    Arr[5]=4
    MyList = LDList.FromArray(Arr)
    LDList.SortByNumber(MyList)
    minArr = LDList.GetAt(MyList,1)
    TextWindow.WriteLine("Minimum is "+minArr)

    Or again without extension:

    Arr[1]=5
    Arr[2]=8
    Arr[3]=7
    Arr[4]=2
    Arr[5]=4
    minArr = Arr[1]
    For i = 2 To Array.GetItemCount(Arr)
      If Arr[i] < Arr[i-1] Then
        minArr = Arr[i]
      EndIf
    EndFor
    TextWindow.WriteLine("Minimum is "+minArr)
    • Marked as answer by Ezra94 Friday, July 11, 2014 1:26 AM
    Thursday, July 10, 2014 9:24 PM
    Answerer

All replies

  • Unless the array is sorted in some way, the only way is checking each element.
    Thursday, July 10, 2014 6:34 PM
    Moderator
  • Ezra94,

    Taking LitDev's advice here is a way of doing it using the LitDev Extension.

    Arr[1]=5
    Arr[2]=8
    Arr[3]=7
    Arr[4]=2
    Arr[5]=4
    Sorted=LDSort.ByValue(arr)
    TextWindow.WriteLine(Sorted)

    Sorted[1] will contain the smallest number.


    JR

    Thursday, July 10, 2014 8:12 PM
    Answerer
  • Or without extensions, check each array value:

    Arr[1]=5
    Arr[2]=8
    Arr[3]=7
    Arr[4]=2
    Arr[5]=4
    minArr = Arr[1]
    For i = 2 To Array.GetItemCount(Arr)
      minArr = Math.Min(minArr,Arr[i])
    EndFor
    TextWindow.WriteLine("Minimum is "+minArr)
    • Marked as answer by Ezra94 Friday, July 11, 2014 1:21 AM
    Thursday, July 10, 2014 8:20 PM
    Moderator
  • Or again with LitDev extension:

    Arr[1]=5
    Arr[2]=8
    Arr[3]=7
    Arr[4]=2
    Arr[5]=4
    MyList = LDList.FromArray(Arr)
    LDList.SortByNumber(MyList)
    minArr = LDList.GetAt(MyList,1)
    TextWindow.WriteLine("Minimum is "+minArr)

    Or again without extension:

    Arr[1]=5
    Arr[2]=8
    Arr[3]=7
    Arr[4]=2
    Arr[5]=4
    minArr = Arr[1]
    For i = 2 To Array.GetItemCount(Arr)
      If Arr[i] < Arr[i-1] Then
        minArr = Arr[i]
      EndIf
    EndFor
    TextWindow.WriteLine("Minimum is "+minArr)
    • Marked as answer by Ezra94 Friday, July 11, 2014 1:26 AM
    Thursday, July 10, 2014 9:24 PM
    Answerer
  • I liked all of the examples. I'm going to test them out in a program I'm writing and see what works. Thank you all!
    Friday, July 11, 2014 1:25 AM
  • Thank you for the example. I haven't tried a program with extension though. I need to download one right?
    Friday, July 11, 2014 1:29 AM
  • Ezra94,

    Yes, you need to download an extension. The LItDev extension is the one I used for the example.

    To get it type smallbasic.com in for the URL in your browser. When the page opens down on the lower right you will see where it says extensions. Click on that and a new page opens. It shows a list of extensions. Choose the LitDev extension and click on it. Another page will open and download . Extract the files as they are in a Zip container. The LitDev.dll and LitDev.xml files are what you need to load into Small Basic.

    Find where Small Basic is loaded on your computer. Create a folder called Lib. Place the 2 files that I mentioned in the Lib Folder. Then start Small Basic. Type in LD and you should see a function called LD3DView pop up in the intellisense. From there you will find a bunch of added functions from the extension.

    By the way, the LitDev extension is the best. It works great and is well supported by LitDev.


    JR

    Friday, July 11, 2014 10:34 AM
    Answerer
  • Thank you for the directions and the explanation.
    Friday, July 11, 2014 2:44 PM