I made a list of random numbers. From that list, how can I find the largest number and the corresponding month?
How to find the largest number from a list of number in a textbox with a for loop?
Collapse
X
-
There are a number of different search algorithms that you can use.
Some of them depend on how you are storing the data. Most of them use some sort of loop...includin g for loops.
If you want to do a sequential search, you loop through every item starting at index 0 and going to the length of the items.
Pseudo code example:
Here is an example of a For...Next loop in vb syntax:Code:let biggestValue be 0 let currentIndex be 0 begin loop get the item at the current index if the current item is bigger than the biggestValue Set the biggestBiggestValue to the current item otherwise, do nothing increase the current index if the index is greater than the length of the items, exit the loop loop
-FrinnyCode:Dim currentIndex As Integer Dim maxIndexOfList As Integer maxIndexOfList = 9 For currentIndex = 0 to maxIndexOfList Step 1 'get the item at the current index 'if the current item is bigger than the biggestValue ' Set the biggestBiggestValue to the current item 'otherwise, do nothing 'note that the currentIndex is automatically incremented by the "step" value provided 'note if no Step is provided, it is assumed to be 1 'note that the loop will end when the currentIndex value is greater than the maxIndexOfList value Next
Last edited by Frinavale; Oct 24 '13, 04:34 PM.
Comment