473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

random number selection and display

33
Hi there,

I'd like to submit this program I'm working on to thr forum.
I want to display 6 randomly generated numbers on 6 command buttons as captions at the click of another button. One number will be selected only once.
Here's my code:
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3.         Dim x As Integer() = New Integer() {x(0), x(1), x(2), x(3), x(4), x(5)}
  4.         Dim m_blnUsed As Boolean() = _
  5.         New Boolean(x.GetUpperBound(0)) {}
  6.  
  7.         Dim objrandom As Random = New Random()
  8.         Dim intrandom As Integer
  9.         Do
  10.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  11.         Loop Until m_blnUsed(intrandom) = False
  12.         m_blnUsed(intrandom) = True
  13.  
  14.         Dim index As Integer
  15.         For index = 0 To 5
  16.             x(index) = x(intrandom)
  17.         Next
  18.  
  19.         Button3.Text = Str(x(0))
  20.         Button4.Text = Str(x(1))
  21.         Button5.Text = Str(x(2))
  22.         Button6.Text = Str(x(3))
  23.         Button7.Text = Str(x(4))
  24.         Button8.Text = Str(x(5))
  25.  
  26.     End Sub
  27.  
  28. End Class
I'm getting an error message
variable 'x' is used before it has been assigned a value. A null reference exception could result at runtime.
at the beginning where I'm declaring and initializing the array of integers x(0)

Can anyone help please.
Thanks
May 3 '07 #1
33 2453
Modibbo
33
forgot to mention I'm using VB2005
May 3 '07 #2
SammyB
807 Expert 512MB
Dim x As Integer() = New Integer() {x(0), x(1), x(2), x(3), x(4), x(5)}
Think what you are telling VB to do:
"I want to declare an array of integers called x, and initialize it to the values in X(0), X(1), ..."
So, VB is saying, duh, you don't have anything in x, so I cannot use it to initialize anything, much less itself.

What do you really want to initialize x to?
May 3 '07 #3
Killer42
8,435 Expert 8TB
Also, I think there's a loop missing there. Unless I'm misreading the code, intrandom is only set once, so all six occurences of x (and hence the buttons) will be set to the same number.
May 3 '07 #4
SammyB
807 Expert 512MB
Also, I think there's a loop missing there. Unless I'm misreading the code, intrandom is only set once, so all six occurences of x (and hence the buttons) will be set to the same number.
Oh, no! This is the same thread as http://www.thescripts.com/forum/thread636691.html with the same mistakes!

OK. I will write an article on random number generation and post it in our new Articles Tab. I'll post a link here afterwards.
May 3 '07 #5
Killer42
8,435 Expert 8TB
Beat you to it, Sammy. :D

Here is the link.
May 4 '07 #6
Modibbo
33
thanks a lot sammyB, killer42 for all your help; i've been going trough all the material you've suggested, even some of it i read before by searching, but still can't get my head round it. nevertheless still trying, here's the modified code
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.  
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.         Dim x As Integer() = New Integer(6) {}
  6.  
  7.         Dim randomgeberator As New Random
  8.         Dim index As Integer
  9.         For index = 0 To 5
  10.             x(index) = randomgeberator.Next(0, 20)
  11.         Next
  12.  
  13.         Button3.Text = Str(x(0))
  14.  
  15.  
  16.         Button4.Text = Str(x(1))
  17.         Button5.Text = Str(x(2))
  18.         Button6.Text = Str(x(3))
  19.         Button7.Text = Str(x(4))
  20.         Button8.Text = Str(x(5))
  21.  
  22.     End Sub
  23.  
  24. End Class
  25.  
i'm having this at runtime on the immediate window
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
now i'm being able to select randomly 6 numbers and display them in the buttons, the bit i can't achieve is to select one number once only which i was trying to do with the boolean array but i'm having trouble to set it up
any help will be so much appreciated
thanks
May 4 '07 #7
Modibbo
33
I've been able to achieve what i wanted with the code
Expand|Select|Wrap|Line Numbers
  1. Dim x As Integer() = New Integer(10) {}
  2.  
  3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  4.  
  5.         Dim m_blnUsed As Boolean() = _
  6.         New Boolean(x.GetUpperBound(0)) {}
  7.  
  8.  
  9.         Dim objrandom As Random = New Random()
  10.         Dim intrandom As Integer
  11.         Dim index As Integer
  12.         Do
  13.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  14.  
  15.         Loop Until m_blnUsed(intrandom) = False
  16.         m_blnUsed(intrandom) = True
  17.  
  18.  
  19.         For index = 0 To 5
  20.             x(index) = intrandom
  21.         Next
  22.  
  23.         Button3.Text = Str(x(0))
  24.  
  25.         Do
  26.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  27.  
  28.         Loop Until m_blnUsed(intrandom) = False
  29.         m_blnUsed(intrandom) = True
  30.  
  31.  
  32.         For index = 0 To 5
  33.             x(index) = intrandom
  34.         Next
  35.         Button4.Text = Str(x(1))
  36.         Do
  37.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  38.  
  39.         Loop Until m_blnUsed(intrandom) = False
  40.         m_blnUsed(intrandom) = True
  41.  
  42.  
  43.         For index = 0 To 5
  44.             x(index) = intrandom
  45.         Next
  46.         Button5.Text = Str(x(2))
  47.         Do
  48.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  49.  
  50.         Loop Until m_blnUsed(intrandom) = False
  51.         m_blnUsed(intrandom) = True
  52.  
  53.  
  54.         For index = 0 To 5
  55.             x(index) = intrandom
  56.         Next
  57.         Button6.Text = Str(x(3))
  58.         Do
  59.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  60.  
  61.         Loop Until m_blnUsed(intrandom) = False
  62.         m_blnUsed(intrandom) = True
  63.  
  64.  
  65.         For index = 0 To 5
  66.             x(index) = intrandom
  67.         Next
  68.         Button7.Text = Str(x(4))
  69.         Do
  70.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  71.  
  72.         Loop Until m_blnUsed(intrandom) = False
  73.         m_blnUsed(intrandom) = True
  74.  
  75.  
  76.         For index = 0 To 5
  77.             x(index) = intrandom
  78.         Next
  79.         Button8.Text = Str(x(5))
  80.  
  81.     End Sub
  82.  
  83. End Class
i'm having this on immediate window
though at run time i'm still getting A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
A first chance exception of type 'System.IndexOutOfRangeException' occurred in 2nd ind asgmt.exe
i'm more concern about the next stage which i don't have any clue, even how to start
what i want is the first of the buttons containing the selected numbers clicked, the number will be copied in the first of a 6 set of labels and so on.
to be more explicit, the 6 buttons will be clicked one at a time and their content copied in the 6 labels as going; the 1st button to be clicked to the 1st label, so on
May 4 '07 #8
Modibbo
33
i've been thinking (may be wrongly & sorry about that incase) there might be a way to set up an array of captions and assign them to the labels and i've been trying hard, but again i have no clue
any help pleasethis what i've been trying to
Expand|Select|Wrap|Line Numbers
  1. doDim y As CaptionButton() = New CaptionButton() {}
  2.  
  3. label1.text="y(0)"
label2.text=y(1)and i'm getting the error message
Declaration expected
at both the last 2 lines of the code (where i'm trying to assign the captions to the labels
May 4 '07 #9
Killer42
8,435 Expert 8TB
What about if you just set up a form-level variable to count the number of times buttons have been clicked. Each time a button is clicked (up to 6, or whatever) you add 1 to that counter and use it to decide which label to update.

As for the way you're assigning the values to the buttons, it's good to see that you got it working, but I believe you can use a bit more loop processing to reduce the size of the code considerably. Remember, the basic idea behind a loop is that you should not need to do something more than once. Anything which you are coding repeatedly (for example, your For x = 0 to 5 loop setting up the x array) can usually be done inside a loop instead.
May 5 '07 #10
Modibbo
33
thanks once more again for helping, as i was saying i don't have any clue as to how to start, would it be possible to give an example of code please
thanks
May 5 '07 #11
Modibbo
33
i've been trying to set up a string array and assign the contents to each of the labels as needed with the code
Expand|Select|Wrap|Line Numbers
  1.  Dim y As String() = New String() {}
  2.  
  3. Label1.Text="y(0)"
  4.  
and getting the error message
Declaration expected
with a wigly line on Label1
May 5 '07 #12
SammyB
807 Expert 512MB
i've been trying to set up a string array and assign the contents to each of the labels as needed with the code
Expand|Select|Wrap|Line Numbers
  1.  Dim y As String() = New String() {}
  2.  
  3. Label1.Text="y(0)"
  4.  
and getting the error message with a wigly line on Label1
You must have deleted Label1
May 5 '07 #13
Modibbo
33
You must have deleted Label1
unless if i don't get you, but i did not delete Label1 as i'm telling you the wigly line is on Label1, so it has to be there to have the wigly line on it
May 5 '07 #14
SammyB
807 Expert 512MB
unless if i don't get you, but i did not delete Label1 as i'm telling you the wigly line is on Label1, so it has to be there to have the wigly line on it
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?
May 5 '07 #15
Modibbo
33
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
May 5 '07 #16
SammyB
807 Expert 512MB
i more confused now as i can see Label1, if i hover over it i get the same error message
WHAT ERROR MESSAGE! :ROFL:
May 5 '07 #17
Modibbo
33
WHAT ERROR MESSAGE! :ROFL:
the error message is
Declaration expected
May 5 '07 #18
Modibbo
33
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
May 5 '07 #19
Modibbo
33
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
May 5 '07 #20
Modibbo
33
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
May 5 '07 #21
Modibbo
33
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
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2.         Dim NoOfElements As Integer, I As Integer, J As Integer, Temp As Integer
  3.         NoOfElements = 6
  4.         For I = 1 To (NoOfElements - 1)
  5.             For J = I To (NoOfElements - 1)
  6.                 If x(I - 1) > x(J) Then
  7.                     Temp = x(I - 1)
  8.                     x(I - 1) = x(J)
  9.                     x(J) = Temp
  10.                 End If
  11.             Next J
  12.         Next I
  13.         Button3.Text = x(0)
  14.  
  15.         Button4.Text = x(1)
  16.  
  17.         Button5.Text = x(2)
  18.  
  19.         Button6.Text = x(3)
  20.  
  21.         Button7.Text = x(4)
  22.  
  23.         Button8.Text = x(5)
it is displaying the No in button8 (the last one) in all of the buttons
May 6 '07 #22
Killer42
8,435 Expert 8TB
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
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2.         Dim NoOfElements As Integer, I As Integer, J As Integer, Temp As Integer
  3.         NoOfElements = 6
  4.         For I = 1 To (NoOfElements - 1)
  5.             For J = I To (NoOfElements - 1)
  6.                 If x(I - 1) > x(J) Then
  7.                     Temp = x(I - 1)
  8.                     x(I - 1) = x(J)
  9.                     x(J) = Temp
  10.                 End If
  11.             Next J
  12.         Next I
  13.         Button3.Text = x(0)
  14.         Button4.Text = x(1)
  15.         Button5.Text = x(2)
  16.         Button6.Text = x(3)
  17.         Button7.Text = x(4)
  18.         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.
May 6 '07 #23
Modibbo
33
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
Expand|Select|Wrap|Line Numbers
  1. Class Form1
  2.     Dim x As Integer() = New Integer(20) {}
  3.  
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.  
  6.         Dim m_blnUsed As Boolean() = _
  7.         New Boolean(x.GetUpperBound(0)) {}
  8.  
  9.  
  10.         Dim objrandom As Random = New Random()
  11.         Dim intrandom As Integer
  12.         Dim index As Integer
  13.         Do
  14.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  15.  
  16.         Loop Until m_blnUsed(intrandom) = False
  17.         m_blnUsed(intrandom) = True
  18.  
  19.  
  20.         For index = 0 To 5
  21.             x(index) = intrandom
  22.         Next
  23.  
  24.         Button3.Text = Str(x(0))
  25.  
  26.         Do
  27.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  28.  
  29.         Loop Until m_blnUsed(intrandom) = False
  30.         m_blnUsed(intrandom) = True
  31.  
  32.  
  33.         For index = 0 To 5
  34.             x(index) = intrandom
  35.         Next
  36.         Button4.Text = Str(x(1))
  37.         Do
  38.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  39.  
  40.         Loop Until m_blnUsed(intrandom) = False
  41.         m_blnUsed(intrandom) = True
  42.  
  43.  
  44.         For index = 0 To 5
  45.             x(index) = intrandom
  46.         Next
  47.         Button5.Text = Str(x(2))
  48.         Do
  49.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  50.  
  51.         Loop Until m_blnUsed(intrandom) = False
  52.         m_blnUsed(intrandom) = True
  53.  
  54.  
  55.         For index = 0 To 5
  56.             x(index) = intrandom
  57.         Next
  58.         Button6.Text = Str(x(3))
  59.         Do
  60.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  61.  
  62.         Loop Until m_blnUsed(intrandom) = False
  63.         m_blnUsed(intrandom) = True
  64.  
  65.  
  66.         For index = 0 To 5
  67.             x(index) = intrandom
  68.         Next
  69.         Button7.Text = Str(x(4))
  70.         Do
  71.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  72.  
  73.         Loop Until m_blnUsed(intrandom) = False
  74.         m_blnUsed(intrandom) = True
  75.  
  76.  
  77.         For index = 0 To 5
  78.             x(index) = intrandom
  79.         Next
  80.         Button8.Text = Str(x(5))
  81.  
  82.         Label2.Text = ""
  83.         Label3.Text = ""
  84.         Label4.Text = ""
  85.         Label5.Text = ""
  86.         Label6.Text = ""
  87.         Label7.Text = ""
  88.     End Sub
  89.  
  90.     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
  91.         Dim index As Integer = sender.ToString.IndexOf(":") + 2
  92.         Dim btnName As Object = sender.ToString.Substring(index)
  93.  
  94.         If Label2.Text = "" Then
  95.             'load the name from btnName into the Text property of the Label
  96.             Label2.Text = btnName
  97.         ElseIf Label3.Text = "" Then
  98.             'load the name from btnName into the Text property of the Label
  99.             Label3.Text = btnName
  100.         ElseIf Label4.Text = "" Then
  101.             'load the name from btnName into the Text property of the Label
  102.             Label4.Text = btnName
  103.         ElseIf Label5.Text = "" Then
  104.             'load the name from btnName into the Text property of the Label
  105.             Label5.Text = btnName
  106.         ElseIf Label6.Text = "" Then
  107.             'load the name from btnName into the Text property of the Label
  108.             Label6.Text = btnName
  109.         ElseIf Label7.Text = "" Then
  110.             'load the name from btnName into the Text property of the Label
  111.             Label7.Text = btnName
  112.         End If
  113.  
  114.     End Sub
  115.  
  116.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  117.  
  118.         Array.Sort(x)
  119.  
  120.         Button3.Text = x(0)
  121.  
  122.         Button4.Text = x(1)
  123.  
  124.         Button5.Text = x(2)
  125.  
  126.         Button6.Text = x(3)
  127.  
  128.         Button7.Text = x(4)
  129.  
  130.         Button8.Text = x(5)
  131.  
  132.     End Sub
  133.  
  134.  
  135. End Class
May 6 '07 #24
Killer42
8,435 Expert 8TB
nearly there!
Keep on plugging away - you'll get 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...
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.

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

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...
Expand|Select|Wrap|Line Numbers
  1.         For index = 0 To 5
  2.             x(index) = intrandom
  3.         Next
  4.  
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...
Expand|Select|Wrap|Line Numbers
  1. For Index = each element of x() array
  2.   Start loop
  3.     Generate a random number
  4.     If it doesn’t match any entry in the array, Then
  5.       Place the value in x(Index)
  6.       Exit the loop
  7.     End If
  8.   End Loop
  9. Next
Then load the values from the array into the buttons. Now they match.
May 7 '07 #25
Modibbo
33
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...
Expand|Select|Wrap|Line Numbers
  1.         For index = 0 To 5
  2.             x(index) = intrandom
  3.         Next
  4.  
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...
Expand|Select|Wrap|Line Numbers
  1. For Index = each element of x() array
  2.   Start loop
  3.     Generate a random number
  4.     If it doesn’t match any entry in the array, Then
  5.       Place the value in x(Index)
  6.       Exit the loop
  7.     End If
  8.   End Loop
  9. 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
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim index As Integer, randomgenerator As New Random, computerchoice As Integer
  3.         For index = 0 To 5
  4.             Do While computerchoice = randomgenerator.Next(0, 19)
  5.                 If computerchoice <> x(index) Then
  6.                     x(index) = computerchoice
  7.                 End If
  8.             Loop
  9.         Next
  10.         Button3.Text = Str(x(0))
  11.         Button4.Text = Str(x(1))
  12.         Button5.Text = Str(x(2))
  13.         Button6.Text = Str(x(3))
  14.         Button7.Text = Str(x(4))
  15.         Button8.Text = Str(x(5))
which is displaying 0 in all the command buttons
And the second
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim index As Integer, randomgenerator As New Random, computerchoice As Integer
  3.         For index = 0 To 5
  4.             computerchoice = randomgenerator.Next(0, 19)
  5.             If computerchoice <> x(index) Then
  6.                 x(index) = computerchoice
  7.             End If
  8.         Next
  9.         Button3.Text = Str(x(0))
  10.         Button4.Text = Str(x(1))
  11.         Button5.Text = Str(x(2))
  12.         Button6.Text = Str(x(3))
  13.         Button7.Text = Str(x(4))
  14.         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
May 7 '07 #26
Killer42
8,435 Expert 8TB
The first
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.   Dim index As Integer, randomgenerator As New Random, computerchoice As Integer
  3.   For index = 0 To 5
  4.     Do While computerchoice = randomgenerator.Next(0, 19)
  5.       If computerchoice <> x(index) Then
  6.         x(index) = computerchoice
  7.       End If
  8.     Loop
  9.   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.
May 7 '07 #27
Modibbo
33
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
Expand|Select|Wrap|Line Numbers
  1. For index = 0 To 5
  2.             Do While index <= 5
  3.                 computerchoice = randomgenerator.Next(0, 21)
  4.                 If computerchoice <> x(index) Then
  5.                     x(index) = computerchoice
  6.                 End If
  7.             Loop
  8.         Next
May 7 '07 #28
Killer42
8,435 Expert 8TB
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...
Expand|Select|Wrap|Line Numbers
  1. For Index = x To y
  2.   Do
  3.     Generate random number
  4.     Look for it in the array
  5.     If not found
  6.       Add it to the array
  7.       Exit Do
  8.     End If
  9.   Loop
  10. 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.
May 7 '07 #29
Modibbo
33
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...
Expand|Select|Wrap|Line Numbers
  1. For Index = x To y
  2.   Do
  3.     Generate random number
  4.     Look for it in the array
  5.     If not found
  6.       Add it to the array
  7.       Exit Do
  8.     End If
  9.   Loop
  10. 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...else 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
Expand|Select|Wrap|Line Numbers
  1.  Public Class Form1
  2.     Dim x As Integer() = New Integer(5) {}
  3.  
  4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  5.  
  6.         Label1.Text = "Select in ascending order by clicking on the numbers"
  7.         Label1.BackColor = Color.Empty
  8.  
  9.         Dim m_blnUsed As Boolean() = _
  10.         New Boolean(20) {}
  11.  
  12.  
  13.         Dim objrandom As Random = New Random()
  14.         Dim intrandom As Integer
  15.  
  16.         Do
  17.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  18.  
  19.         Loop Until m_blnUsed(intrandom) = False
  20.         m_blnUsed(intrandom) = True
  21.  
  22.         'add the random value into the x array
  23.         x(0) = intrandom
  24.  
  25.  
  26.         Button3.Text = Str(x(0))
  27.  
  28.         Do
  29.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  30.  
  31.         Loop Until m_blnUsed(intrandom) = False
  32.         m_blnUsed(intrandom) = True
  33.  
  34.         'add the random value into the x array
  35.         x(1) = intrandom
  36.  
  37.         Button4.Text = Str(x(1))
  38.         Do
  39.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  40.  
  41.         Loop Until m_blnUsed(intrandom) = False
  42.         m_blnUsed(intrandom) = True
  43.  
  44.         'add the random value into the x array
  45.         x(2) = intrandom
  46.  
  47.         Button5.Text = Str(x(2))
  48.         Do
  49.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  50.  
  51.         Loop Until m_blnUsed(intrandom) = False
  52.         m_blnUsed(intrandom) = True
  53.  
  54.         'add the random value into the x array
  55.         x(3) = intrandom
  56.  
  57.         Button6.Text = Str(x(3))
  58.         Do
  59.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  60.  
  61.         Loop Until m_blnUsed(intrandom) = False
  62.         m_blnUsed(intrandom) = True
  63.  
  64.         'add the random value into the x array
  65.         x(4) = intrandom
  66.  
  67.         Button7.Text = Str(x(4))
  68.         Do
  69.             intrandom = objrandom.Next(0, m_blnUsed.Length)
  70.  
  71.         Loop Until m_blnUsed(intrandom) = False
  72.         m_blnUsed(intrandom) = True
  73.  
  74.         'add the random value into the x array
  75.         x(5) = intrandom
  76.  
  77.         Button8.Text = Str(x(5))
  78.  
  79.         Label2.Text = ""
  80.         Label3.Text = ""
  81.         Label4.Text = ""
  82.         Label5.Text = ""
  83.         Label6.Text = ""
  84.         Label7.Text = ""
  85.     End Sub
  86.  
  87.     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
  88.         Dim index As Integer = sender.ToString.IndexOf(":") + 2
  89.         Dim btnName As Object = sender.ToString.Substring(index)
  90.  
  91.         If Label2.Text = "" Then
  92.             'load the name from btnName into the Text property of the Label
  93.             Label2.Text = btnName
  94.         ElseIf Label3.Text = "" Then
  95.             'load the name from btnName into the Text property of the Label
  96.             Label3.Text = btnName
  97.         ElseIf Label4.Text = "" Then
  98.             'load the name from btnName into the Text property of the Label
  99.             Label4.Text = btnName
  100.         ElseIf Label5.Text = "" Then
  101.             'load the name from btnName into the Text property of the Label
  102.             Label5.Text = btnName
  103.         ElseIf Label6.Text = "" Then
  104.             'load the name from btnName into the Text property of the Label
  105.             Label6.Text = btnName
  106.         ElseIf Label7.Text = "" Then
  107.             'load the name from btnName into the Text property of the Label
  108.             Label7.Text = btnName
  109.         End If
  110.  
  111.     End Sub
  112.  
  113.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  114.  
  115.         Array.Sort(x)
  116.  
  117.         Button3.Text = x(0)
  118.  
  119.         Button4.Text = x(1)
  120.  
  121.         Button5.Text = x(2)
  122.  
  123.         Button6.Text = x(3)
  124.  
  125.         Button7.Text = x(4)
  126.  
  127.         Button8.Text = x(5)
  128.  
  129.         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
  130.  
  131.             Label1.Text = "Correct, well done!"
  132.             Label1.BackColor = Color.Green
  133.         Else
  134.             Label1.Text = "Sorry, incorrect!"
  135.             Label1.BackColor = Color.Red
  136.         End If
  137.     End Sub
  138.  
  139.  
  140. End Class
May 8 '07 #30
Killer42
8,435 Expert 8TB
i want to check the answers with the if...then...else statement at the end of the program, but i don't know, it its always running the else part...
Have you interrupted the code and examined the values at that point, to see what doesn't match?
May 9 '07 #31
Modibbo
33
Have you interrupted the code and examined the values at that point, to see what doesn't match?
as a matter of fact, i don't know how to interrupt the code and always wondered how to do it
May 10 '07 #32
SammyB
807 Expert 512MB
as a matter of fact, i don't know how to interrupt the code and always wondered how to do it
Before running the code, click on the grey-bar to the left of your code, beside the line of code you are interested in. Then when you run your code, it will "break" (temporally stop, waiting for you) just before it executes that line. You can examine objects and variables by hovering over them, step to the next statement (F10), step into a subroutine(F11), or resume execution(F5).
May 10 '07 #33
Killer42
8,435 Expert 8TB
Before running the code, click on the grey-bar...
You should learn to use the debugging facilities built into VB - they are essential for debugging, or just making sure you understand, your code.

They allow you to stop the execution, examine and change values, and step through the execution so that you can watch exactly what is executing. Just seeing precisely which statements are executed can often show up bugs. "Oh, I thought that IF would be FALSE...".
May 10 '07 #34

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: tao_benz | last post by:
Hi: My system generates a bunch of integers, about 1000. There is no any relationship between those integers; the smallest one might only contain 1 digit and the biggest one might contain 6...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
4
by: Baz | last post by:
I want to randomly select some files from a source directory & copy them to a target folder for testing. How can I select files from the source directory randomly? I have thought about getting a...
2
by: RJN | last post by:
Hi I have to randomly pickup records from database for some quality check of data. Say the data base has about 1000 records, I have to randomly select about 50 records. Can someone suggest me...
40
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't...
2
by: wisten | last post by:
This program is to create a simple application to generate random number between 1 and 50. additional: when the user click on the Start button, a random number will be displayed every 300ms.Display...
5
by: JuAn2226 | last post by:
How do I create database which will store the random number that generated before I reset the random number and how do I check that new random number after I reset is exists in database. If exist...
7
by: Suprim | last post by:
I am using a timer to call a random number range from 1 to 50 and display it in a label. How can i display the number without repeating the number that have been call out? For example, if number...
4
by: jhernandy | last post by:
I have built a Table using HTML and would like to have a random number genereator, genereate numbers in each of the 5 boxes on the table with no duplicates between 1-50, I have a problem getting the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.