random number selection and display

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Modibbo
    New Member
    • Apr 2007
    • 33

    #16
    Originally posted by SammyB
    No, it has to not be there to have a wigly line. :D When you hover over the wigly line, what is the error message?
    i more confused now as i can see Label1, if i hover over it i get the same error message
    Declaration expected

    Comment

    • SammyB
      Recognized Expert Contributor
      • Mar 2007
      • 807

      #17
      Originally posted by Modibbo
      i more confused now as i can see Label1, if i hover over it i get the same error message
      WHAT ERROR MESSAGE! :ROFL:

      Comment

      • Modibbo
        New Member
        • Apr 2007
        • 33

        #18
        Originally posted by SammyB
        WHAT ERROR MESSAGE! :ROFL:
        the error message is
        Declaration expected

        Comment

        • Modibbo
          New Member
          • Apr 2007
          • 33

          #19
          Originally posted by Modibbo
          the error message is
          hands up sammyB, i've just realised what i did was starting a new line after ending the precedind sub, so VB was telling me hang on you did not declare the new sub you're starting; a proof of how confused i am with this program, still struggling though with it, gonna be there, never give up

          Comment

          • Modibbo
            New Member
            • Apr 2007
            • 33

            #20
            guys, how would i go about choosing between the buttons, for the 1st clicked store its content in the 1st label, the 2d button to the 2nd label, so on.
            my problem is th choose between the first button which is clicked , whichever is this first, it can be the button in the line to be clicked first, or the last one in the line, it doesn't matter, the 1st clicked, its value assigned to 1st label, and so on

            Comment

            • Modibbo
              New Member
              • Apr 2007
              • 33

              #21
              Originally posted by Modibbo
              guys, how would i go about choosing between the buttons, for the 1st clicked store its content in the 1st label, the 2d button to the 2nd label, so on.
              my problem is th choose between the first button which is clicked , whichever is this first, it can be the 3rd button in the line to be clicked first, or the last one in the line, it doesn't matter, the 1st clicked, its value assigned to 1st label, and so on
              any suggestion, any hint, thanks

              Comment

              • Modibbo
                New Member
                • Apr 2007
                • 33

                #22
                i've been able to display the Nos in the labels in order of theclick of the buttons
                now i want to sort the Nos and display the sorted values in the buttons (from 2 to 6) in order to arrage them in ascending order
                here's my code
                Code:
                Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                        Dim NoOfElements As Integer, I As Integer, J As Integer, Temp As Integer
                        NoOfElements = 6
                        For I = 1 To (NoOfElements - 1)
                            For J = I To (NoOfElements - 1)
                                If x(I - 1) > x(J) Then
                                    Temp = x(I - 1)
                                    x(I - 1) = x(J)
                                    x(J) = Temp
                                End If
                            Next J
                        Next I
                        Button3.Text = x(0)
                
                        Button4.Text = x(1)
                
                        Button5.Text = x(2)
                
                        Button6.Text = x(3)
                
                        Button7.Text = x(4)
                
                        Button8.Text = x(5)
                it is displaying the No in button8 (the last one) in all of the buttons

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #23
                  Originally posted by Modibbo
                  i've been able to display the Nos in the labels in order of theclick of the buttons
                  now i want to sort the Nos and display the sorted values in the buttons (from 2 to 6) in order to arrage them in ascending order
                  here's my code
                  Code:
                  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                          Dim NoOfElements As Integer, I As Integer, J As Integer, Temp As Integer
                          NoOfElements = 6
                          For I = 1 To (NoOfElements - 1)
                              For J = I To (NoOfElements - 1)
                                  If x(I - 1) > x(J) Then
                                      Temp = x(I - 1)
                                      x(I - 1) = x(J)
                                      x(J) = Temp
                                  End If
                              Next J
                          Next I
                          Button3.Text = x(0)
                          Button4.Text = x(1)
                          Button5.Text = x(2)
                          Button6.Text = x(3)
                          Button7.Text = x(4)
                          Button8.Text = x(5)
                  it is displaying the No in button8 (the last one) in all of the buttons
                  Sorry, been busy over the weekend.

                  It looks as though you've been gradually working your way through the problems, which is great. :)

                  Are you saying that after you use this sort code, you are getting the same value displayed in all of the labels?

                  Hm...

                  Ah! I remember now. I don't think this sort is to blame. If I remember correctly, your code which creates these numbers used the x() array in rather a strange way. I think you will find that if you interrupt your code and examine the array just before the sort, you'll find that they already contain the duplicated number. Assuming that is the case, you need to fix the original load of the values, not the sort.

                  Comment

                  • Modibbo
                    New Member
                    • Apr 2007
                    • 33

                    #24
                    nearly there!
                    i know as killer42 made the remark, it's not the best code but as long as it's working i'll try and improve it when finished, not to forget i've been programming only 2 months, enjoying it though.
                    now the last bit, after sorting the array with Array.Sort, all of the 6 buttons display always 0
                    here's my full code
                    Code:
                    Class Form1
                        Dim x As Integer() = New Integer(20) {}
                    
                        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                    
                            Dim m_blnUsed As Boolean() = _
                            New Boolean(x.GetUpperBound(0)) {}
                    
                    
                            Dim objrandom As Random = New Random()
                            Dim intrandom As Integer
                            Dim index As Integer
                            Do
                                intrandom = objrandom.Next(0, m_blnUsed.Length)
                    
                            Loop Until m_blnUsed(intrandom) = False
                            m_blnUsed(intrandom) = True
                    
                    
                            For index = 0 To 5
                                x(index) = intrandom
                            Next
                    
                            Button3.Text = Str(x(0))
                    
                            Do
                                intrandom = objrandom.Next(0, m_blnUsed.Length)
                    
                            Loop Until m_blnUsed(intrandom) = False
                            m_blnUsed(intrandom) = True
                    
                    
                            For index = 0 To 5
                                x(index) = intrandom
                            Next
                            Button4.Text = Str(x(1))
                            Do
                                intrandom = objrandom.Next(0, m_blnUsed.Length)
                    
                            Loop Until m_blnUsed(intrandom) = False
                            m_blnUsed(intrandom) = True
                    
                    
                            For index = 0 To 5
                                x(index) = intrandom
                            Next
                            Button5.Text = Str(x(2))
                            Do
                                intrandom = objrandom.Next(0, m_blnUsed.Length)
                    
                            Loop Until m_blnUsed(intrandom) = False
                            m_blnUsed(intrandom) = True
                    
                    
                            For index = 0 To 5
                                x(index) = intrandom
                            Next
                            Button6.Text = Str(x(3))
                            Do
                                intrandom = objrandom.Next(0, m_blnUsed.Length)
                    
                            Loop Until m_blnUsed(intrandom) = False
                            m_blnUsed(intrandom) = True
                    
                    
                            For index = 0 To 5
                                x(index) = intrandom
                            Next
                            Button7.Text = Str(x(4))
                            Do
                                intrandom = objrandom.Next(0, m_blnUsed.Length)
                    
                            Loop Until m_blnUsed(intrandom) = False
                            m_blnUsed(intrandom) = True
                    
                    
                            For index = 0 To 5
                                x(index) = intrandom
                            Next
                            Button8.Text = Str(x(5))
                    
                            Label2.Text = ""
                            Label3.Text = ""
                            Label4.Text = ""
                            Label5.Text = ""
                            Label6.Text = ""
                            Label7.Text = ""
                        End Sub
                        
                        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click
                            Dim index As Integer = sender.ToString.IndexOf(":") + 2
                            Dim btnName As Object = sender.ToString.Substring(index)
                    
                            If Label2.Text = "" Then
                                'load the name from btnName into the Text property of the Label
                                Label2.Text = btnName
                            ElseIf Label3.Text = "" Then
                                'load the name from btnName into the Text property of the Label
                                Label3.Text = btnName
                            ElseIf Label4.Text = "" Then
                                'load the name from btnName into the Text property of the Label
                                Label4.Text = btnName
                            ElseIf Label5.Text = "" Then
                                'load the name from btnName into the Text property of the Label
                                Label5.Text = btnName
                            ElseIf Label6.Text = "" Then
                                'load the name from btnName into the Text property of the Label
                                Label6.Text = btnName
                            ElseIf Label7.Text = "" Then
                                'load the name from btnName into the Text property of the Label
                                Label7.Text = btnName
                            End If
                    
                        End Sub
                    
                        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                    
                            Array.Sort(x)
                    
                            Button3.Text = x(0)
                    
                            Button4.Text = x(1)
                    
                            Button5.Text = x(2)
                    
                            Button6.Text = x(3)
                    
                            Button7.Text = x(4)
                    
                            Button8.Text = x(5)
                    
                        End Sub
                    
                    
                    End Class

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #25
                      Originally posted by Modibbo
                      nearly there!
                      Keep on plugging away - you'll get there.

                      Originally posted by Modibbo
                      i know as killer42 made the remark, it's not the best code but as long as it's working i'll try and improve it when finished...
                      Although it's probably very poor programming practice, I tend to do that, too. Throw together something (a "proof of concept") that works ASAP, then worry about the niceties like documenting, commenting, naming standards, nice structure, user interface, etc etc etc.

                      Originally posted by Modibbo
                      ...not to forget i've been programming only 2 months, enjoying it though.
                      That's the spirit! :) You're doing fine.

                      Originally posted by Modibbo
                      now the last bit, after sorting the array with Array.Sort, all of the 6 buttons display always 0
                      Honestly, I would completely disregard the sorting and so on, until you get the initial load of the array worked out. At present, the contents of your array have nothing to do with the contents of your command buttons.

                      Consider the following, in your code...
                      Code:
                              For index = 0 To 5
                                  x(index) = intrandom
                              Next
                      This piece of code says to place the exact same value (intrandom) in every occurrence of the x() array. If you actually meant to do this (unlikely), there would be no point using an array – you could just use a single variable, x. In any case, next time you do it, you replace the entire array, so there's no record of what you have just placed there.

                      What you need to do, I think, is to go back and examine the underlying logic to set up the array. I believe it should go more like this...
                      Code:
                      For Index = [I]each element of [B]x()[/B] array[/I]
                        Start loop
                          Generate a random number
                          If it doesn’t match any entry in the array, Then
                            Place the value in x(Index)
                            Exit the loop
                          End If
                        End Loop
                      Next
                      Then load the values from the array into the buttons. Now they match.

                      Comment

                      • Modibbo
                        New Member
                        • Apr 2007
                        • 33

                        #26
                        Originally posted by Killer42
                        Keep on plugging away - you'll get there.

                        Although it's probably very poor programming practice, I tend to do that, too. Throw together something (a "proof of concept") that works ASAP, then worry about the niceties like documenting, commenting, naming standards, nice structure, user interface, etc etc etc.

                        That's the spirit! :) You're doing fine.

                        Honestly, I would completely disregard the sorting and so on, until you get the initial load of the array worked out. At present, the contents of your array have nothing to do with the contents of your command buttons.

                        Consider the following, in your code...
                        Code:
                                For index = 0 To 5
                                    x(index) = intrandom
                                Next
                        This piece of code says to place the exact same value (intrandom) in every occurrence of the x() array. If you actually meant to do this (unlikely), there would be no point using an array – you could just use a single variable, x. In any case, next time you do it, you replace the entire array, so there's no record of what you have just placed there.

                        What you need to do, I think, is to go back and examine the underlying logic to set up the array. I believe it should go more like this...
                        Code:
                        For Index = [I]each element of [B]x()[/B] array[/I]
                          Start loop
                            Generate a random number
                            If it doesn’t match any entry in the array, Then
                              Place the value in x(Index)
                              Exit the loop
                            End If
                          End Loop
                        Next
                        Then load the values from the array into the buttons. Now they match.
                        Thanks a lot killer42
                        This is what I came up with, 2 attempts
                        The first
                        Code:
                        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                                Dim index As Integer, randomgenerator As New Random, computerchoice As Integer
                                For index = 0 To 5
                                    Do While computerchoice = randomgenerator.Next(0, 19)
                                        If computerchoice <> x(index) Then
                                            x(index) = computerchoice
                                        End If
                                    Loop
                                Next
                                Button3.Text = Str(x(0))
                                Button4.Text = Str(x(1))
                                Button5.Text = Str(x(2))
                                Button6.Text = Str(x(3))
                                Button7.Text = Str(x(4))
                                Button8.Text = Str(x(5))
                        which is displaying 0 in all the command buttons
                        And the second
                        Code:
                        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                                Dim index As Integer, randomgenerator As New Random, computerchoice As Integer
                                For index = 0 To 5
                                    computerchoice = randomgenerator.Next(0, 19)
                                    If computerchoice <> x(index) Then
                                        x(index) = computerchoice
                                    End If
                                Next
                                Button3.Text = Str(x(0))
                                Button4.Text = Str(x(1))
                                Button5.Text = Str(x(2))
                                Button6.Text = Str(x(3))
                                Button7.Text = Str(x(4))
                                Button8.Text = Str(x(5))
                        which is displaying one number more than once, ie 3 displayed in button4 & button6

                        Both of the two solutions above are displaying always 0 in all the buttons after sorting the array

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #27
                          Originally posted by Modibbo
                          The first
                          Code:
                          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                            Dim index As Integer, randomgenerator As New Random, computerchoice As Integer
                            For index = 0 To 5
                              [B]Do While computerchoice = randomgenerator.Next(0, 19)[/B]
                                If computerchoice <> x(index) Then
                                  x(index) = computerchoice
                                End If
                              Loop
                            Next
                          I'd like you to examine this one, and see whether you can figure out why it doesn't work. As a hint, I've highlighted the line where I think you've mainly got "the wrong end of the stick".

                          Remember, to successfully debug a program, in many cases the key is to think like a computer. That means you forget for the moment about what you want to achieve at the end, and simply step through precisely what each statement does.

                          Comment

                          • Modibbo
                            New Member
                            • Apr 2007
                            • 33

                            #28
                            Originally posted by Killer42
                            I'd like you to examine this one, and see whether you can figure out why it doesn't work. As a hint, I've highlighted the line where I think you've mainly got "the wrong end of the stick".

                            Remember, to successfully debug a program, in many cases the key is to think like a computer. That means you forget for the moment about what you want to achieve at the end, and simply step through precisely what each statement does.
                            i'm running out of ideas, i know i need a condition after the while (what would it be, that i'm not sure), anyway, i've turned around the loop in so many positions (do...., loop until; do while.....loop, for.....next); i even don't get why do we need this loop in a loop (the outer loop for....next)
                            all i know is i need a push now
                            thanks
                            this is one of my try
                            Code:
                            For index = 0 To 5
                                        Do While index <= 5
                                            computerchoice = randomgenerator.Next(0, 21)
                                            If computerchoice <> x(index) Then
                                                x(index) = computerchoice
                                            End If
                                        Loop
                                    Next

                            Comment

                            • Killer42
                              Recognized Expert Expert
                              • Oct 2006
                              • 8429

                              #29
                              I don't remember what I suggested earlier, but you don't necessarily have to have a condition on a loop. (Have a look at the awesome power of nested loops :)). You could do something like...
                              Code:
                              For Index = x To y
                                [B]Do[/B]
                                  Generate random number
                                  Look for it in the array
                                  [B]If[/B] not found
                                    Add it to the array
                                    Exit Do
                                  [B]End If[/B]
                                [B]Loop[/B]
                              Next
                              :D Now you know how "infinite loops" come to exist. If the code in the middle fails to trigger the Exit Do, then of course this code will run forever. Well, until you interrupt it anyway.

                              I'll be on lunch in about 4 hours, should be able to help more then.

                              Comment

                              • Modibbo
                                New Member
                                • Apr 2007
                                • 33

                                #30
                                Originally posted by Killer42
                                I don't remember what I suggested earlier, but you don't necessarily have to have a condition on a loop. (Have a look at the awesome power of nested loops :)). You could do something like...
                                Code:
                                For Index = x To y
                                  [B]Do[/B]
                                    Generate random number
                                    Look for it in the array
                                    [B]If[/B] not found
                                      Add it to the array
                                      Exit Do
                                    [B]End If[/B]
                                  [B]Loop[/B]
                                Next
                                :D Now you know how "infinite loops" come to exist. If the code in the middle fails to trigger the Exit Do, then of course this code will run forever. Well, until you interrupt it anyway.

                                I'll be on lunch in about 4 hours, should be able to help more then.
                                Thanks a lot killer42, i really appreciated it.
                                Here's my code now, which is not too bad, with help i must acknowledge.
                                Now the very last bit, i want to check the answers with the if...then...els e statement at the end of the program, but i don't know, it its always running the else part, it is in all cases displaying the incorrect message on the red background here's my final code
                                Code:
                                 Public Class Form1
                                    Dim x As Integer() = New Integer(5) {}
                                
                                    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                                
                                        Label1.Text = "Select in ascending order by clicking on the numbers"
                                        Label1.BackColor = Color.Empty
                                
                                        Dim m_blnUsed As Boolean() = _
                                        New Boolean(20) {}
                                
                                
                                        Dim objrandom As Random = New Random()
                                        Dim intrandom As Integer
                                
                                        Do
                                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                                
                                        Loop Until m_blnUsed(intrandom) = False
                                        m_blnUsed(intrandom) = True
                                
                                        'add the random value into the x array
                                        x(0) = intrandom
                                
                                
                                        Button3.Text = Str(x(0))
                                
                                        Do
                                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                                
                                        Loop Until m_blnUsed(intrandom) = False
                                        m_blnUsed(intrandom) = True
                                
                                        'add the random value into the x array
                                        x(1) = intrandom
                                
                                        Button4.Text = Str(x(1))
                                        Do
                                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                                
                                        Loop Until m_blnUsed(intrandom) = False
                                        m_blnUsed(intrandom) = True
                                
                                        'add the random value into the x array
                                        x(2) = intrandom
                                
                                        Button5.Text = Str(x(2))
                                        Do
                                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                                
                                        Loop Until m_blnUsed(intrandom) = False
                                        m_blnUsed(intrandom) = True
                                
                                        'add the random value into the x array
                                        x(3) = intrandom
                                
                                        Button6.Text = Str(x(3))
                                        Do
                                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                                
                                        Loop Until m_blnUsed(intrandom) = False
                                        m_blnUsed(intrandom) = True
                                
                                        'add the random value into the x array
                                        x(4) = intrandom
                                
                                        Button7.Text = Str(x(4))
                                        Do
                                            intrandom = objrandom.Next(0, m_blnUsed.Length)
                                
                                        Loop Until m_blnUsed(intrandom) = False
                                        m_blnUsed(intrandom) = True
                                
                                        'add the random value into the x array
                                        x(5) = intrandom
                                
                                        Button8.Text = Str(x(5))
                                
                                        Label2.Text = ""
                                        Label3.Text = ""
                                        Label4.Text = ""
                                        Label5.Text = ""
                                        Label6.Text = ""
                                        Label7.Text = ""
                                    End Sub
                                    
                                    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click, Button8.Click
                                        Dim index As Integer = sender.ToString.IndexOf(":") + 2
                                        Dim btnName As Object = sender.ToString.Substring(index)
                                
                                        If Label2.Text = "" Then
                                            'load the name from btnName into the Text property of the Label
                                            Label2.Text = btnName
                                        ElseIf Label3.Text = "" Then
                                            'load the name from btnName into the Text property of the Label
                                            Label3.Text = btnName
                                        ElseIf Label4.Text = "" Then
                                            'load the name from btnName into the Text property of the Label
                                            Label4.Text = btnName
                                        ElseIf Label5.Text = "" Then
                                            'load the name from btnName into the Text property of the Label
                                            Label5.Text = btnName
                                        ElseIf Label6.Text = "" Then
                                            'load the name from btnName into the Text property of the Label
                                            Label6.Text = btnName
                                        ElseIf Label7.Text = "" Then
                                            'load the name from btnName into the Text property of the Label
                                            Label7.Text = btnName
                                        End If
                                
                                    End Sub
                                
                                    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
                                
                                        Array.Sort(x)
                                
                                        Button3.Text = x(0)
                                
                                        Button4.Text = x(1)
                                
                                        Button5.Text = x(2)
                                
                                        Button6.Text = x(3)
                                
                                        Button7.Text = x(4)
                                
                                        Button8.Text = x(5)
                                
                                        If Label2.Text = Button3.Text And Label3.Text = Button4.Text And Label4.Text = Button5.Text And Label5.Text = Button6.Text And Label6.Text = Button7.Text And Label7.Text = Button8.Text Then
                                
                                            Label1.Text = "Correct, well done!"
                                            Label1.BackColor = Color.Green
                                        Else
                                            Label1.Text = "Sorry, incorrect!"
                                            Label1.BackColor = Color.Red
                                        End If
                                    End Sub
                                
                                
                                End Class

                                Comment

                                Working...