Unable to edit array data in For Each Loop - vb.net

Like title, running this code shows an array with all 0 values in it.
I have tried using integer array too but I can't
Dim woohoo(9) As String
Dim count As Integer = 2
For Each number As String In woohoo
number = CStr(count)
count += 2
Next
For Each number As Integer In woohoo
Console.WriteLine(number)
Next
2,4,6,8,10...20

You need to do this to SET the values of an array:
Dim wooho(9) As Integer
Dim count As Integer = 2
For i As Integer = 0 To wooho.Count - 1
wooho(i) = count
count += 2
Console.WriteLine(wooho(i))
Next
And this to GET the values:
For Each number As Integer In wooho
Console.WriteLine(number)
Next

I suggest using a list of Integers for this.
Try this:
Dim woohoo As New List (Of Integer)
Dim count As Integer = 2
For i As Integer = 0 To 9
woohoo.Items.Add (count)
count += 2
Next
For Each number As Integer In woohoo
Console.WriteLine(CStr(number))
Next

Related

How to calculate average of arrays from array list?

I have an ArrayList in VB.NET.
For example ArrayList has two arrays: AList(arr,arr):
Dim AList As ArrayList = New ArrayList((1,2,3,4,5,6,7,8,9,10)
(2,3,4,5,6,7,8,9,10,11))
All I want is average of given arrays so:
So final Array will be (1.5,2.5,3.5,4.5,5.5.........10.5)
Note: Arraylist count and length of arrays will variable.
I tried the following code:
For k = 0 To 9
Dim sum As Short
Dim a As Array
For z = 0 To 1
sum = sum + AList.Item(a(k))
Next
finalarr(k) = sum / 2
RichTextBox1.AppendText(finalarr(k))
Next
Dim i As Integer = 0
Dim count As Inetger = 0
Dim avgList As List(Of Integer) = New List(Of Integer)()
For Each arr As Integer() In AList
i = 0
count += 1
For Each thing as Integer In arr
i += 1
If avgList.Count >= i Then
avgList.Item(i) = ((avgList.Item(i) + thing) / count)
Else
avgList.Add(i)
End If
Next
Next

Arithmetic Quiz On VB

I'm a beginner and need help with a school project. I'm trying to get the scores to show from highest to lowest and the average score. How can I do this? My current code is:
Dim filename As String
Dim numberofrecords As Short
Dim countofloop As Short
Dim one, two, three As Short
filename = "N:\Class1.dat"
FileOpen(1, filename, OpenMode.Random, , , Len(OneStudentScore))
numberofrecords = LOF(1) / Len(OneStudentScore)
countofloop = 0
Dim scores(numberofrecords) As Integer
Dim names(numberofrecords) As String
Do While (Not EOF(1))
FileGet(1, OneStudentScore)
one = OneStudentScore.score1
two = OneStudentScore.score2
three = OneStudentScore.score3
names(countofloop) = OneStudentScore.Name
If one > two Then
If one > three Then
'1st score is the biggest
scores(countofloop) = one
Else
scores(countofloop) = three
End If
Else
If two > three Then
scores(countofloop) = two
Else
scores(countofloop) = three
End If
End If
countofloop = countofloop + 1
Loop
'sort array into score order
Array.Sort(scores, names)
'this allows the arrays to be in descending order - highest first
Array.Reverse(scores)
Array.Reverse(names)
Dim displayeditem As String
lstClass1.Items.Clear()
'display the arrays in a list box
For i = 0 To countofloop
displayeditem = names(i) & " " & scores(i)
lstClass1.Items.Add(displayeditem)
FileClose()
Next
End Sub
End Class
I didn't run the code but it would appear that you are properly sorting the array to go from highest to lowest. If you just need the average then it should just be a matter of adding up the elements in the scores array and dividing by the number of elements. Something like:
Dim total As Integer = 0
Dim numScores As Integer = 0
For i As Integer = 0 To scores.Length - 1
total += scores(i)
numScores += 1
Next
Dim average As Integer
average = total / numScores

vb.net generate random number and string

I want to generate a random number of 6 intergers in vb.net which should be number and also some times alphanumerice.Like ist 4 or 5 numbers should be numbers and next should be alphnumeric.I created both numbers separatedly.like this
Public Function rand() As String
'Number
Dim rng As Random = New Random
Dim number As Integer = rng.Next(1, 1000000)
Dim digits As String = number.ToString("000000")
'Alphnumeric
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim r As New Random
Dim sb As New StringBuilder
For i As Integer = 1 To 5
Dim idx As Integer = r.Next(0, 35)
sb.Append(s.Substring(idx, 1))
Next
End Function
Now Any One Give Me Idea that in this function can i return number and some times string or may b a single instances which create both numbers and strings.
The first problem to solve is the random number generator. You should use only one instance and not multiple instances that gives back the same sequence if called in short time distance. Then it is
difficult to say what 'something' means in your requirements, but supposing you are fine with rougly 70% numbers and 30% a mix of numbers and strings then you call the random generator to decide for a sequence of only numbers or a mixed one. Based on the output of the random selection of the sequence build from the appropriate string
' A global unique random generator'
Dim rng As Random = New Random
Sub Main
Console.WriteLine(rand())
Console.WriteLine(rand())
Console.WriteLine(rand())
Console.WriteLine(rand())
End Sub
Public Function rand() As String
Dim sb As New StringBuilder
' Selection of pure numbers sequence or mixed one
Dim pureNumbers = rng.Next(1,11)
if pureNumbers < 7 then
' Generate a sequence of only digits
Dim number As Integer = rng.Next(1, 1000000)
Dim digits As String = number.ToString("000000")
For i As Integer = 1 To 6
Dim idx As Integer = rng.Next(0, digits.Length)
sb.Append(digits.Substring(idx, 1))
Next
else
' Generate a sequence of digits and letters
Dim s As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
For i As Integer = 1 To 6
Dim idx As Integer = rng.Next(0, 36)
sb.Append(s.Substring(idx, 1))
Next
End if
return sb.ToString()
End Function

Generation of random string based on two characters in VB.NET

I'm trying to create a predefined number of rows, each having a predefined number of characters. The characters allowed are only 0's and 1's and are randomly generated.
I have achieved this with this code:
Dim _allowedChars As String = "01"
Dim randomNumber As New Random()
Dim chars As List(Of Char) = New List(Of Char)
Dim allowedCharCount As Integer = _allowedChars.Length
For o As Integer = 0 To rows - 1
For a As Integer = 0 To rowCharacters - 1
chars.Add(_allowedChars.Chars(CInt(Fix((_allowedChars.Length) * randomNumber.NextDouble()))))
Next a
For a As Integer = 0 To chars.Count - 1
'results for each row go here in a textbox line
Next a
chars = New List(Of Char)
'row o finished, new line and go for next row
Next o
This works great, an example output of setting 5 rows and 5 chars (only consisted of randomly generated 0's or 1's) is shown here:
11101
00000
11011
00000
01011
I now want to add an extra twist to it: specify the minimum percentage of 1's for each row, i.e. "even though randomly distributed, each row should have at least 20% of 1's". The percentage is based on the length of the chars in each row (variable rowCharacters in the code).
Anyone who could help me out on this one?
Thanks!
Dim _allowedChars As String = "01"
Dim randomNumber As New Random()
Dim chars As List(Of Char) = New List(Of Char)
Dim allowedCharCount As Integer = _allowedChars.Length
'
For o As Integer = 0 To rows - 1
Do
For a As Integer = 0 To rowCharacters - 1
chars.Add(_allowedChars.Chars(CInt(Fix((_allowedChars.Length) * randomNumber.NextDouble()))))
Next a
For a As Integer = 0 To chars.Count - 1
'results for each row go here in a textbox line
Next a
chars = New List(Of Char)
'row o finished, new line and go for next row
Loop Until IsValidPercent(chars, 20)
Next o
Function IsValidPercent(chars As List(Of Char), ReqPercentage as integer) as boolean
'
Dim counter as integer = 0
Dim qtyones as integer = 0
'
IsValidPercent = False
for counter = 0 to chars.count -1
if chars(counter) = "1" Then qtyones = qtyones + 1
next
if qtyones >= ((chars.count/100)*ReqPercentage) Then IsValidPercent = True
'
End Function

Split integer into array VB

Good afternoon,
How can i split a value and insert it into a array in VB?
An example:
The initial value is 987654321.
Using a for loop i need to insert the value as something like this:
Position(1) = 9 'The first number from the splited integer
Position(2) = 8 'The second number from the splited integer
and so on...
Thank you.
This code is untested:
Dim x As Integer = 987654321
Dim s As String = x.ToString
Dim a(s.Length) As String
For i As Integer = 0 To s.Length - 1
a(i) = s.Substring(i, 1)
Next i
You could try:
Dim number As Integer = 987654321
Dim strText As String = number.ToString()
Dim charArr() As Char = strText.ToCharArray()
Once the numbers are separated, you can then pull them out from this array and convert them back to numbers if you need to.
Dim number As Integer = 987654321
Dim digits() As Integer = number.ToString().Cast(Of Integer)().ToArray()
Will show any number separated in 3 different message box.
You can make a function with the example to better suit your purpose.
Sub GetNumber()
Dim x As Integer, s As String
x = 987
s = LTrim(Str(x))
For i = 1 To Len(s)
MsgBox Mid(s, i, 1)
Next i
End Sub
I know this is an old question, but here is the most elegant solution I could get to work:
Dim key As Integer = 987654321
Dim digits() As Integer = System.Array.ConvertAll(Of Char, Integer)(key.ToString.ToCharArray, Function(c As Char) Integer.Parse(c.ToString))