Results 1 to 5 of 5

Thread: Searching an Array?

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Question Searching an Array?

    Ok, basically I've created a random number which is stored in Index and this value is then added as the first value in an array. This process is repeated of making a random index number then storing it in the next value of an array.

    I have managed to do this, but I dont know how to search the array full of index values to see if when a random number has been created if that number is in the array then a new random number has to be generated.

    I already have some of the code for this bit,

    VB Code:
    1. Randomize
    2.     Index = Int(Rnd * 10) + 3
    3.         Do While *the value index is in the array*
    4.             Index = Int(Rnd * 10) + 3
    5.         Loop


    The array is called QuestionsDone(13)

    Can anyone help me?

    Thanks.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Searching an Array?

    You can loop the array to check if the number is already generated, however this will take time especially at the end of the array since there is only one number left that can be generated and you might need to randomly select many numbers before this is done. I can show you an easier way of creating an array with random numbers, but I'll need a bit more information before I do that since I'm a bit confused by your code.

    You say that you have an array named QuestionsDone(13) which (unless you have set Option Base 1 at the top of your code) means that you have 14 indexies in that array, QuestionsDone(0) To QuestionsDone(13). However the random number you generate with this code:
    VB Code:
    1. Index = Int(Rnd * 10) + 3
    will get a random number between 3 and 12 (a total of 10 different numbers can be generated here). So how can you fill an array with 14 items with 10 different numbers and you don't want any duplicates?

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2006
    Posts
    39

    Re: Searching an Array?

    yes you're right it should be QuestionsDone(11) but that was because I was had changed bits of my code it doesn't really matter.
    Basically what I'm doing is creating a random number with index because each value of index holds a different question. So therefore when each random index value is created a random question is called. Dont ask why index holds the question, it would make sense if you saw my program just accept it.

    But of course I dont want the same question being repeated, so I thought putting the index values in an array would mean that next time round if that number was generated then the array would be searched to see if that number was already there. If it has then a new index value is created until the value is not found in the array.

    Is there a better way of doing this?

    Thanks in advance.

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Searching an Array?

    You can use Filter() to find matches. But it must be an Array of strings..

    Syntax:
    Filter(SourceArray, Match, [include] as Boolean, Compare Method)

    Example on how to use Filer..
    This use 2 comandbuttons and 2 Listsboxes.
    The listbox is filled with even numbers, so select a number from list1 and click command1, it will show in list2 all matches, command2 will show all 'no matches' of the number you selected.
    VB Code:
    1. Private StArray() As String
    2. Private StTempArray() As String
    3.  
    4. Private Sub Form_Load()
    5.     Dim i As Long
    6.    
    7.     For i = 1 To 40
    8.         ReDim Preserve StArray(i)
    9.         StArray(i) = CStr(i * 2)
    10.         List1.AddItem CStr(i)
    11.     Next
    12. End Sub
    13.  
    14. Private Sub subSearch(pVal As Boolean)
    15.     Dim i As Long
    16.    
    17.     If List1.ListIndex = -1 Then Exit Sub
    18.     '3rd parameter determines matches or no matches
    19.     StTempArray = Filter(StArray, List1.List(List1.ListIndex), pVal)
    20.     List2.Clear
    21.     For i = 0 To UBound(StTempArray)
    22.         List2.AddItem StTempArray(i)
    23.     Next
    24.     Erase StTempArray
    25. End Sub
    26.  
    27. Private Sub Command1_Click()
    28.     subSearch True
    29. End Sub
    30.  
    31. Private Sub Command2_Click()
    32.     subSearch False
    33. End Sub

    EDIT: If you have your questions in an array, you don't even need to worry about indexes, you can use filter() with the question itself, searching the array of 'questions done' to know if it is already there.
    Last edited by jcis; Feb 18th, 2006 at 03:32 PM.

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Searching an Array?

    I'm still confused... How many questions do you have and what's the range of the numbers you want to store? Are there 10, 11 or 12 questions? Are the questions numbered started from 1 or a higher number?

    Anyway... The following will return an array (0 To 9) containing the numbers 1 to 10 in a random order.
    VB Code:
    1. Public Function GetRandomArray() As Integer()
    2.     ReDim iArr(9) As Integer
    3.     Dim iTemp As Integer
    4.     Dim iRand As Integer
    5.     Dim i As Integer
    6.     Randomize
    7.     'save the numbers 1 through 10
    8.     For i = 0 To 9
    9.         iArr(i) = i + 1
    10.     Next
    11.     'now scramble up the values
    12.     For i = 0 To 9
    13.         Do
    14.             iRand = Int(Rnd * 10)
    15.         Loop While iRand <> i
    16.         'Swap the values in the array
    17.         iTemp = iArr(iRand)
    18.         iArr(iRand) = iArr(i)
    19.         iArr(i) = iTemp
    20.     Next
    21.     'Return the array
    22.     GetRandomArray = iArr
    23. End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width