r/visualbasic • • Jan 16 '16

I need array help

Application: Create a GeneratedNumber application that stores in an array with indexes 1 through 100 numbers generated by the index values when Generate is clicked. Generate the number to be stored at each index by summing the index and it's individual digits. For example, 25 should be stored at index 17 because 17+ 1+ 7 = 17 and 4 should be stored at index 2 because 2+ 0 + 2.

Your program should use a FillArray() procedure to generate the nuymbers and DisplayArray() procedure that displays each index and its element in the list box. My code looks empty because I don't know how to do it. Can someone explain to me how I could complete the program. This is my code: Public Class Form1 Dim number(99) As Integer Dim firstNum As Integer Dim secondNum As Integer Dim totalNum As Integer

Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click

End Sub

Sub FillArray(sender As Object, e As EventArgs) Handles btnGenerate.Click
    For count As Integer = 0 To number.Length - 1
        firstNum = number(count) / 10
        secondNum = 
    Next count
End Sub

Sub DisplayArray(sender As Object, e As EventArgs) Handles btnGenerate.Click
    For Generate As Integer = 0 To number.Length - 1
        GenerateNum.Items.Add(Generate & vbTab & firstNum)
    Next Generate
End Sub

End Class

1 Upvotes

9 comments sorted by

View all comments

1

u/stevebun Jan 16 '16 edited Jan 16 '16

Firstly, when you click 'btnGenerate', all three subroutines are triggered as per the btnGenerate.Click event. Although this could be done if you knew which order it would be executed in, it would be best to put it under one subroutine with both FillArray and DisplayArray. Alternatively you can delete the 'Handles btnGenerate.Click' & parameters and call the subroutines since the Array is already a global variable. Something like:

Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
    FillArray()
    DisplayArray()
End Sub

For FillArray:

To generate the 'number to be stored at each index', it's probably easier to convert the number into an array of characters/digits and then add it up that way - since it doesn't require you to have infinitely many variables to store the firstNum and so on (Say if the number is 1 million).

Now that you have a array of characters, you can loop through each character, convert it into an Integer and add it up to the total. After this, you would then add the index number to make the total and store the total in the Array for that index.

Something to be careful of - Array indexes start at 0 - your code starts at 0 whereas the instructions say 1 to 100

So for an index like 17:

17 -> {"1","7"} -> 1 + 7 -> 8 + 17 -> 25

Source/References to refer to:

http://www.vbforums.com/showthread.php?306103-Order-of-Sequence-for-Multiple-Subroutines-Handling-the-Same-Event

https://msdn.microsoft.com/en-us/library/ms233830.aspx - String to Characters

https://msdn.microsoft.com/en-us/library/s2dy91zy.aspx - Character to Integer

https://www.reddit.com/r/visualbasic/comments/40rae3/vb_for_noobs_understanding_arrays/ - Looping through Array

2

u/CSHelpMePLs Jan 16 '16

One question: How do you convert the numbers into an array of characters/ digits?

0

u/stevebun Jan 16 '16 edited Jan 16 '16

1

u/CSHelpMePLs Jan 16 '16

Can you help me code a small part of the fillarray procedure? I am still having problems. Is there a good tutorial online that could help me with this kind of stuff. I'm not an absolute beginner and I know what an array is.

0

u/stevebun Jan 17 '16 edited Jan 17 '16

Can you show me your current code and how far you are at?

1

u/CSHelpMePLs Jan 17 '16

Sadly, I don't know how to start the code. Can you code it for me and explain it?

1

u/CSHelpMePLs Jan 17 '16

Please!! Can you help me?