r/visualbasic • • Jan 23 '16

DynamicNumbers

I only finished a third of the requirements. I need to make a findLowest function that returns the index of the lowest value of the arrayusing the code that I created. I also need to make a sort() procedure that repeateldy finds the lowest value in an array A, removes it and adds it to array T. When all the values of A have been moved, the elements of T are copied to A wth an assginment statement and a loop.. AddItem() and RemoveItem() is a hint. I only completed the part where it says to create an array of random numbers in a listbox. Here is what I finished so far:

Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
    Dim number(txtBox.Text)
    Dim rand As New Random
    For count As Integer = 0 To txtBox.Text - 1
        RandomNumbers.Items.Add(count & vbTab & rand.Next(1, 99))
        number(count) = RandomNumbers.Items(count)
    Next count
End Sub

Can you guys tell me how to complete this program? I'm a beginner for visual basic programming.

6 Upvotes

29 comments sorted by

1

u/PostalElf VB.Net Intermediate Jan 23 '16

Create a variable to hold the smallest value, and another to hold the index. Loop through your array, each time comparing the current item to the smallest value: is it smaller? Yes, store the value and index. No, move on.

At the end of the loop you will have both the value and the index.

1

u/GRIFFIN-PETER Jan 23 '16

Can you give me an example? I don't think I know how to do it. I need a findLowest() function and sort() procedure.

1

u/PostalElf VB.Net Intermediate Jan 23 '16

You should have learnt how to create a variable, how to compare values in variables, and how to loop or iterate through an array. That's all you need to know to do what I outlined above, which is the logic for findLowest().

Good luck! :)

1

u/GRIFFIN-PETER Jan 24 '16

wow! You help dman with the code and you don't help me with code. Pls help me. I'm still a noob. I only started visual basic last month. pls help me.

2

u/PostalElf VB.Net Intermediate Jan 24 '16

I gave /u/dman-no-one a code snippet because they asked a syntax specific question. He demonstrated an understanding of his program's flow and needed help only with the implementation, not with the actual programming bit.

You, on the other hand... you're basically outright asking someone to write the code for you. That's not cool. I've given you the logic and tools you need to write your code, so why don't you actually try writing it? If you've tried, show me what you've tried. Explain to me where and how you've failed, and I'll help you further.

1

u/GRIFFIN-PETER Jan 24 '16

I need help. I think I did my part wrong. In the textbox I need to enter the size of an array. When I want to call a function using that array(txtBox.text), it doesn't work. I get an error message. How do I create an array using the textbox information to get random numbers. My code is still up there.

1

u/PostalElf VB.Net Intermediate Jan 24 '16

Write the code doing this:

Create a variable to hold the smallest value, and another to hold the index. Loop through your array, each time comparing the current item to the smallest value: is it smaller? Yes, store the value and index. No, move on. At the end of the loop you will have both the value and the index.

1

u/GRIFFIN-PETER Jan 24 '16

No, there is something wrong with my random numbers part of the code. Can you check to see if it's correct? Is there a way you can improve it?

1

u/PostalElf VB.Net Intermediate Jan 24 '16
Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click
    Dim number() as Integer     'this is how you declare an array of integers
    Dim rand As New Random
    'everything in a textbox.text is a string, you'll need to use CInt() to convert it into an integer
    For count As Integer = 0 To CInt(txtBox.Text) - 1
        RandomNumbers.Items.Add(count & vbTab & rand.Next(1, 99))
        number(count) = RandomNumbers.Items(count)
    Next count
End Sub

1

u/GRIFFIN-PETER Jan 24 '16

For some reason, I got an error message. It says variable "number" is used before it has been assigned by a value. A null reference exception could result at runtime.

→ More replies (0)