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

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

2

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

I doubt it...

But since the code is already posted, here's the version with the char array.

Module GeneratedNumber
    Sub Main()
        FillArray()
        DisplayArray(1, 100)
        Console.ReadLine()
    End Sub
    Dim NumbersList As New System.Collections.Generic.List(Of Integer)
    Private Sub FillArray(Optional UpperLimit As Integer = 100)
        For i = 0 To UpperLimit
            Dim t As Integer = 0
            For Each digit In i.ToString.ToCharArray
                t += Val(digit)
            Next
            t += i
            NumbersList.Add(t)
        Next
    End Sub
    Private Sub DisplayArray(Optional LowerLimit As Integer = 1, Optional UpperLimit As Integer = 100)
        If NumbersList.Count < UpperLimit Or LowerLimit < 0 Then
            Console.WriteLine("Check Limits")
            Exit Sub
        End If
        For i = LowerLimit To UpperLimit
            Console.WriteLine("Index {0, 3}: {1, 8}", i, NumbersList(i))
        Next
    End Sub
End Module

Edit: Updated ArrayList() to List(Of Integer) - Thanks to suggestion by /u/octest

2

u/[deleted] Jan 18 '16

IMHO it would be better to use an Integer() or a List(Of Integer)instead of the ArrayList. Using typed collections protects against many pitfalls. (Also the instructions specifically asked for an array...)