Results 1 to 4 of 4

Thread: maths question - extracting numbers

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Hi there,

    I use the code below to generate 2 groups of numbers, A and B, into my picturebox. The groups, or sets, are determined randomly. A random question (one of 3 questions) is loaded below the sets.

    For example, when command1 is clicked, the picturebox might say:

    ------

    A = {1,3,4,6,9)
    B = {2,4,7,9}

    What is A intersect B?

    ------

    What I want to happen next is when they click command2, it gives them the answer in my label, label1.

    In this case the answer would be: A intersect B is {4,9}.

    Does anybody know how I can do this. I know it's some sort of loop or something, but I'm not sure of the code.

    The other questions are:

    What is A union B? (a list of the contents of A and B, without repetition

    What is A less B? (a list of numbers in A that are not in B)

    I attach my code below, in the hope that someone might know.

    Thanks for any help!

    -----------------

    ' 2 command buttons, a label, a picturebox

    Private Sub Command1_Click()

    Dim numbertoprint As Integer
    Dim numbertoprint2 As Integer
    Dim intSeqNbr(1 To 8) As Integer
    Dim intSeqNbr2(1 To 8) As Integer
    Dim intX As Integer
    Dim intX2 As Integer
    Dim strPrintData As String
    Dim strPrintData2 As String
    Dim strCommaOrNot As String
    Dim setsquestion As Integer

    Picture1.Cls
    Randomize
    'there will be two sets, A and B
    'decide the amount of numbers, from 4 to 8, to print for
    'both sets in the Picturebox
    numbertoprint = Int(Rnd * 5) + 4
    numbertoprint2 = Int(Rnd * 5) + 4
    'decide the space there will be between numbers
    intSeqNbr(1) = Int(Rnd * 4) + 1
    intSeqNbr2(1) = Int(Rnd * 4) + 1
    'intX loops through the amount of numbers selected and
    'adds an amount onto each one
    For intX = 2 To 8
    'add an amount, from 1 to 3, onto each number
    intSeqNbr(intX) = intSeqNbr(intX - 1) + Int(Rnd * 3) + 1
    Next
    For intX2 = 2 To 8
    intSeqNbr2(intX2) = intSeqNbr2(intX2 - 1) + Int(Rnd * 3) + 1
    Next

    strPrintData = "A = {"
    strPrintData2 = "B = {"

    'For every number chosen in the sequence....
    For intX = 1 To numbertoprint
    '....add a comma at the end of it, except the last number
    strCommaOrNot = IIf(intX = 1, "", ",")

    'this is the random sequence of numbers, commas and all
    strPrintData = strPrintData & strCommaOrNot & intSeqNbr(intX)

    Next

    For intX2 = 1 To numbertoprint2

    strCommaOrNot = IIf(intX2 = 1, "", ",")

    strPrintData2 = strPrintData2 & strCommaOrNot & intSeqNbr2(intX2)
    Next

    'Now print the sets in the Picturebox
    Picture1.Print strPrintData & "}" & vbCr & strPrintData2 & "}"

    'There are 3 different questions that can be asked.
    'choose 1 at random.
    setsquestion = Int(Rnd * 3)
    Select Case setsquestion

    Case "0"
    Picture1.Print vbCr & vbCr & vbCr & "What is A intersect B?"

    Case "1"
    Picture1.Print vbCr & vbCr & vbCr & "What is A union B?"

    Case "2"
    Picture1.Print vbCr & vbCr & vbCr & "What is A less B?"
    End Select

    End Sub

  2. #2
    What you need to do is set up a loop within a loop.

    Try the following code to get a list of numbers that are in A and not B:

    Code:
    Dim i as integer, j as integer, k as integer, l as integer
    Dim numbersA(1 to 5) as integer, numbersB(1 to 5) as integer
    Dim Unison(1 to 10) as integer, Different(1 to 10) as integer
    
    i=0
    j=0
    k=0
    l=0
    
    For i = 0 to UBound(numbersA)
      For j = 0 to Ubound(numbersB)
        If numbersA(i) <> numbersB(j) Then
          Different(k) = numbersA(i)
          k=k+1
          Exit For
        End If
      Next j
    Next i
    For your unison query try:
    Code:
    For i = 0 to UBound(numbersA)
      For j = 0 to UBound(numbersB)
        For k = 0 to UBound(Unison)
          If numbersA(i) = numbersB(j) Then
            If numbersA(i) <> Unison(k) Then
              Unison(k) = numbersA(i)
              Exit For
            End If
          End If
        Next k
      Next j
    Next i
    Hope this works, or at least helps you out.

    Matt

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Glasgow,Scotland
    Posts
    281

    Smile


    ca9mbu,

    I had a look at your code but couldn't get it working. However, I found a way to do it. The problem is that it's very long winded and I know there's a quicker way to loop around it or something, but I don't know how. Could someone give me a hand?

    Basically, IntSeqNbr is the amount of numbers that appear at random for set A - this can be from 4 to 8 numbers. IntSeqNbr is for set B.

    What the code below does is print the numbers that set A has in common with B.

    E.g:

    'the question:

    A = {3,5,8,9,11}
    B = {6,8,9,12}

    What is A intersect B?

    ' the answer:

    8,9

    Here's my code:

    'As I said, it's very long and would appreciate if someone 'could help 'me shorten it. Thanks.

    Private Sub Command2_Click()

    If intSeqNbr(1) = intSeqNbr2(1) Then
    Picture1.Print intSeqNbr(1) & ",";
    End If
    If intSeqNbr(1) = intSeqNbr2(2) Then
    Picture1.Print intSeqNbr(1) & ",";
    End If
    If intSeqNbr(1) = intSeqNbr2(3) Then
    Picture1.Print intSeqNbr(1) & ",";
    End If
    If intSeqNbr(1) = intSeqNbr2(4) Then
    Picture1.Print intSeqNbr(1) & ",";
    End If
    If intSeqNbr(1) = intSeqNbr2(5) Then
    Picture1.Print intSeqNbr(1) & ",";
    End If
    If intSeqNbr(1) = intSeqNbr2(6) Then
    Picture1.Print intSeqNbr(1) & ",";
    End If
    If intSeqNbr(1) = intSeqNbr2(7) Then
    Picture1.Print intSeqNbr(1) & ",";
    End If
    If intSeqNbr(1) = intSeqNbr2(8) Then
    Picture1.Print intSeqNbr(1) & ",";
    End If
    If intSeqNbr(2) = intSeqNbr2(1) Then
    Picture1.Print intSeqNbr(2) & ",";
    End If
    If intSeqNbr(2) = intSeqNbr2(2) Then
    Picture1.Print intSeqNbr(2) & ",";
    End If
    If intSeqNbr(2) = intSeqNbr2(3) Then
    Picture1.Print intSeqNbr(2) & ",";
    End If
    If intSeqNbr(2) = intSeqNbr2(4) Then
    Picture1.Print intSeqNbr(2) & ",";
    End If
    If intSeqNbr(2) = intSeqNbr2(5) Then
    Picture1.Print intSeqNbr(3) & ",";
    End If
    If intSeqNbr(2) = intSeqNbr2(6) Then
    Picture1.Print intSeqNbr(3) & ",";
    End If
    If intSeqNbr(2) = intSeqNbr2(7) Then
    Picture1.Print intSeqNbr(3) & ",";
    End If
    If intSeqNbr(2) = intSeqNbr2(8) Then
    Picture1.Print intSeqNbr(3) & ",";
    End If

    'etc...etc....until we get to intseqnbr(8) = intseqnbr2(8)

    End Sub

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    Try this

    Code:
    Private Sub Intersect(SetA() As Variant, SetB As Variant)
    Dim intStartDifference As Integer
    Dim i As Integer
    Dim j As Integer
    
    intStartDifference = LBound(SetA) - LBound(SetB)
    
    For i = LBound(SetA) To UBound(SetA)
    
        For j = i + intStartDifference To UBound(SetB)
        
            If SetA(i) = SetB(j) Then _
                Picture1.Print SetA(i) & "."
        
        Next j
        
    Next i
    
    
    End Sub

    then you only need one line

    Code:
    Call Intersect(intSeqNbr, intSeqNbr2)

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