locked
Smallbasic - Passing values from sub routine or comparing/matching values in two arrays RRS feed

  • Question

  • Hi,

    Is there anyway in SB, that I can pass an array from a sub routine into the main sub routine? If not, then what's the easiest way to compare two arrays and match numbers...it's a sort of lottery application, whereby two series of random numbers are drawn and then compared to see if there are 3, 4, 5 or 6 matches...

     

    Thanks in advance...

     

    Wendy

    Monday, June 13, 2011 8:39 PM

Answers

  • ALL  variables in SmallBasic are 'global scope' - that is they exist, can be created, modified used or deleted anywhere, in a subroutine, the main calling program - anywhere.

    So we can create 2 arrays of random numbers, then use a subroutine to compare them, or create one array (perhaps user chosen numbers) and create a subroutine that compares them against a random set created in the subroutine; both the original user array, the random array and any comparison statistics will be present and available anywhere in the code (after they are created).

    Simple example:

    'Create a list of 10 random numbers
    For i = 1 To 10
      list1[i] = Math.GetRandomNumber(10)
    EndFor

    'Call a sub to test the match against a randomly generated second list
    testMatch()
    'Output the match count and both list values
    TextWindow.WriteLine("There are "+count+" matches.")
    For i = 1 To 10
      TextWindow.WriteLine("List1= "+list1[i]+" : List2= "+list2[i])
    EndFor

    'Count the number of matches of list1 and new list2 arrays
    Sub testMatch
      count = 0
      For i = 1 To 10
        list2[i] = Math.GetRandomNumber(10)
        If (list1[i] = list2[i]) Then
          count = count+1
        EndIf
      EndFor
    EndSub

    If this isn't clear then post a small example of what you are trying to do.

     

    • Proposed as answer by Kirkkaf Wednesday, June 15, 2011 8:07 AM
    • Marked as answer by paz67 Thursday, June 16, 2011 8:23 AM
    • Unmarked as answer by paz67 Friday, February 10, 2012 11:59 PM
    • Marked as answer by Ed Price - MSFTMicrosoft employee Tuesday, October 23, 2012 12:35 AM
    Monday, June 13, 2011 9:00 PM
  • Its not clear what you want exactly - I assume due to it being called lottery that a match occurs when numbers in the same position in the 2 lists match - with this assumption, you just need to lest if the two lists have the same value at each of the indexes.

    'Create a list of 10 random numbers
    For i = 1 To 10
      list1[i] = Math.GetRandomNumber(10)
    EndFor
    
    'Create a second list of 10 random numbers
    For i = 1 To 10
      list2[i] = Math.GetRandomNumber(10)
    EndFor
    
    'Call a sub to test the match between the 2 lists
    testMatch()
    'Output the match count and both list values
    TextWindow.WriteLine("There are "+count+" matches.")
    For i = 1 To 10
      TextWindow.WriteLine("List1= "+list1[i]+" : List2= "+list2[i])
    EndFor
    
    'Count the number of matches of list1 and new list2 arrays
    Sub testMatch
      count = 0
      For i = 1 To 10
        If (list1[i] = list2[i]) Then
          count = count+1
        EndIf
      EndFor
    EndSub
    
    
    If this is not the assumption, the you will need to give a clearer description and example of two lists and what the result should be.

    Saturday, February 11, 2012 2:31 PM
  • No worries, here's the working code, I sorted it:

    'Create a list of  6  random numbers - My Ticket
    For i = 1 To 6
      Ticket[i] = Math.GetRandomNumber(49)
      Draw[i] = Math.GetRandomNumber(49)
    EndFor


    'Create a list of  6  random numbers - Draw Numbers
    'For n = 1 To 6
     ' Draw[i] = Math.GetRandomNumber(49)
    'EndFor

    'Call a sub to test the match against a randomly generated second list
    TicketMatch()
    'Output the match count and both list values

    TextWindow.WriteLine("There are "+ match +" matches.")
    For i = 1 To 6
      TextWindow.WriteLine("Ticket " + Ticket[i] +" : Draw = "+Draw[i])
    EndFor

     

    'Count the number of matches of Ticket and LotteryDraws
    Sub TicketMatch
      match = 0
      counter1 = 0
     
      While counter1 < 6
        counter2 = 1
        While counter2 <6
          If Ticket[counter1] = Draw[counter2]  Then
            match = match + 1
          EndIf
          counter2 = counter2 + 1
        EndWhile
        counter1 = counter1 + 1
      Endwhile
     
    EndSub

     


     


    Wendy

    Saturday, February 11, 2012 3:34 PM

All replies

  • ALL  variables in SmallBasic are 'global scope' - that is they exist, can be created, modified used or deleted anywhere, in a subroutine, the main calling program - anywhere.

    So we can create 2 arrays of random numbers, then use a subroutine to compare them, or create one array (perhaps user chosen numbers) and create a subroutine that compares them against a random set created in the subroutine; both the original user array, the random array and any comparison statistics will be present and available anywhere in the code (after they are created).

    Simple example:

    'Create a list of 10 random numbers
    For i = 1 To 10
      list1[i] = Math.GetRandomNumber(10)
    EndFor

    'Call a sub to test the match against a randomly generated second list
    testMatch()
    'Output the match count and both list values
    TextWindow.WriteLine("There are "+count+" matches.")
    For i = 1 To 10
      TextWindow.WriteLine("List1= "+list1[i]+" : List2= "+list2[i])
    EndFor

    'Count the number of matches of list1 and new list2 arrays
    Sub testMatch
      count = 0
      For i = 1 To 10
        list2[i] = Math.GetRandomNumber(10)
        If (list1[i] = list2[i]) Then
          count = count+1
        EndIf
      EndFor
    EndSub

    If this isn't clear then post a small example of what you are trying to do.

     

    • Proposed as answer by Kirkkaf Wednesday, June 15, 2011 8:07 AM
    • Marked as answer by paz67 Thursday, June 16, 2011 8:23 AM
    • Unmarked as answer by paz67 Friday, February 10, 2012 11:59 PM
    • Marked as answer by Ed Price - MSFTMicrosoft employee Tuesday, October 23, 2012 12:35 AM
    Monday, June 13, 2011 9:00 PM
  • Hi,

    The only problem with:

    Sub testMatch
      count = 0
      For i = 1 To 10
        list2[i] = Math.GetRandomNumber(10)
        If (list1[i] = list2[i]) Then
          count = count+1
        EndIf
      EndFor
    EndSub

    Is that it is only comparing values of the same indexed number. I need each value in List1 to check against each value in List2,

    e.g. match L1[1] -> L2 [1-6], match L1[2] ->L2[1-6] etc.

    Thanks


    Wendy

    Friday, February 10, 2012 11:40 PM
  • I can't seem to get this code working properly...if it's possible?

    'Count the number of matches of Ticket and LotteryDraws
    Sub TicketMatch
      match = 0
      counter1 = 0
     
      While counter1 < 6
        counter2 = 1
        While counter2 <6
          If  array.containsvalue(Ticket,counter1) = array.containsvalue(Draw,counter2)  Then
            match = match + 1
          EndIf
          counter2 = counter2 + 1
        EndWhile
        counter1 = counter1 + 1
      Endwhile
     
    EndSub


    Wendy

    Saturday, February 11, 2012 8:20 AM
  • Its not clear what you want exactly - I assume due to it being called lottery that a match occurs when numbers in the same position in the 2 lists match - with this assumption, you just need to lest if the two lists have the same value at each of the indexes.

    'Create a list of 10 random numbers
    For i = 1 To 10
      list1[i] = Math.GetRandomNumber(10)
    EndFor
    
    'Create a second list of 10 random numbers
    For i = 1 To 10
      list2[i] = Math.GetRandomNumber(10)
    EndFor
    
    'Call a sub to test the match between the 2 lists
    testMatch()
    'Output the match count and both list values
    TextWindow.WriteLine("There are "+count+" matches.")
    For i = 1 To 10
      TextWindow.WriteLine("List1= "+list1[i]+" : List2= "+list2[i])
    EndFor
    
    'Count the number of matches of list1 and new list2 arrays
    Sub testMatch
      count = 0
      For i = 1 To 10
        If (list1[i] = list2[i]) Then
          count = count+1
        EndIf
      EndFor
    EndSub
    
    
    If this is not the assumption, the you will need to give a clearer description and example of two lists and what the result should be.

    Saturday, February 11, 2012 2:31 PM
  • No worries, here's the working code, I sorted it:

    'Create a list of  6  random numbers - My Ticket
    For i = 1 To 6
      Ticket[i] = Math.GetRandomNumber(49)
      Draw[i] = Math.GetRandomNumber(49)
    EndFor


    'Create a list of  6  random numbers - Draw Numbers
    'For n = 1 To 6
     ' Draw[i] = Math.GetRandomNumber(49)
    'EndFor

    'Call a sub to test the match against a randomly generated second list
    TicketMatch()
    'Output the match count and both list values

    TextWindow.WriteLine("There are "+ match +" matches.")
    For i = 1 To 6
      TextWindow.WriteLine("Ticket " + Ticket[i] +" : Draw = "+Draw[i])
    EndFor

     

    'Count the number of matches of Ticket and LotteryDraws
    Sub TicketMatch
      match = 0
      counter1 = 0
     
      While counter1 < 6
        counter2 = 1
        While counter2 <6
          If Ticket[counter1] = Draw[counter2]  Then
            match = match + 1
          EndIf
          counter2 = counter2 + 1
        EndWhile
        counter1 = counter1 + 1
      Endwhile
     
    EndSub

     


     


    Wendy

    Saturday, February 11, 2012 3:34 PM