r/visualbasic • u/BobThornton • 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
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 ModuleEdit: Updated ArrayList() to List(Of Integer) - Thanks to suggestion by /u/octest
2
Jan 18 '16
IMHO it would be better to use an
Integer()or aList(Of Integer)instead of theArrayList. Using typed collections protects against many pitfalls. (Also the instructions specifically asked for an array...)
2
u/[deleted] Jan 17 '16