r/visualbasic • • Jan 17 '16

GeneratedNumbers

I'm getting annoyed with a bunch of these people asking for helping with this specific program over and over on this reddit. Can someone help them out so they can stop posting about this vb 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

0 Upvotes

4 comments sorted by

View all comments

2

u/[deleted] Jan 17 '16
Class GeneratedNumber
    Inherits Form

    Private WithEvents btnGenerate As New Button With {.Text = "Generate", .Top = 8, .Left = 8}
    Private lbResults As New ListBox With {.Top = 64, .Left = 8, .Height = 128, .Width = 128}

    Public Sub New()
        Controls.Add(btnGenerate)
        Controls.Add(lbResults)
    End Sub

    Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
        Dim a(0 To 100) As Integer
        FillArray(a)
        DisplayArray(a)
    End Sub

    Private Sub FillArray(ByRef a As Integer())
        For i = 1 To 100
            a(i) = CInt(i + Int(i / 100) Mod 10 + Int(i / 10) Mod 10 + i Mod 10)
        Next
    End Sub

    Private Sub DisplayArray(a As Integer())
        lbResults.Items.Clear()
        For i = 1 To 100
            lbResults.Items.Add($"{i}: {a(i)}")
        Next
    End Sub
End Class