VB.NET Count bits, 1's are counted as 1, Zero's are counted together in a sequence between 1's - vb.net

Looking to count bits in a sequence.
1's are counted as 1, Zero's are counted together in a sequence between 1's
If you look at this picture
The output should be 2,1,5,1,1,3,2,1
Here is my code it works on 80% of numbers sometimes it messes up.
dim bitmask() as byte
redim bitmask(15)
bitmask(0) = 0
bitmask(1) = 0
bitmask(2) = 1
bitmask(3) = 0
bitmask(4) = 0
bitmask(5) = 0
bitmask(6) = 0
bitmask(7) = 0
bitmask(8) = 1
bitmask(9) = 1
bitmask(10) = 0
bitmask(11) = 0
bitmask(12) = 0
bitmask(13) = 1
bitmask(14) = 0
bitmask(15) = 1
Public Function GetBitCount() As Byte()
Dim count As Byte = 0
Dim bitcounts As New List(Of Byte)
Dim indexOfNext As Integer = 0
Dim totalCounted As Integer = 0
While True
indexOfNext = Array.IndexOf(bitmask, CByte(1), indexOfNext + 1)
If indexOfNext > 0 Then
If indexOfNext - totalCounted = 1 Then
bitcounts.Add(2)
bitcounts.Add(1)
ElseIf indexOfNext - totalCounted > 0 Then
bitcounts.Add(IIf(totalCounted > 0, indexOfNext - totalCounted, indexOfNext))
ElseIf indexOfNext - totalCounted > 1 Then
bitcounts.Add(2)
bitcounts.Add(1)
Else
bitcounts.Add(1)
bitcounts.Add(1)
End If
If totalCounted = 0 Then bitcounts.Add(1)
totalCounted = indexOfNext + 1
Else
Exit While
End If
End While
If totalCounted - 1 > 2 AndAlso totalCounted - 1 < bitmask.Length - 1 Then
bitcounts.Add(1)
bitcounts.Add((bitmask.Length - 1) - (totalCounted - 1))
ElseIf totalCounted - 1 <= 2 AndAlso totalCounted - 1 < bitmask.Length - 1 Then
bitcounts.Add((bitmask.Length - 1) - (totalCounted - 1))
Else
bitcounts.Add(1)
End If
If count > 0 Then bitcounts.Add(count)
Return bitcounts.ToArray()
End Function

Solution
Public Function GetBitCount() As Byte()
Dim i As Integer = 0
Dim count As Byte = 0
Dim bitcounts As New List(Of Byte)
For i = 0 To bitmask.Length - 1
If bitmask(i) = 1 Then
bitcounts.Add(count)
count = 0
End If
count += 1
Next
If count > 0 Then bitcounts.Add(count)
Return bitcounts.ToArray()
End Function

Related

bounding data to mschart so i can return non-data point-values

I'm trying to build a chart composed of up-to-100% stacked columns, each with 4 series and, once built, upon hovering upon the series, return all bound data (in this case, user names)
I'm very close to what i want, but the tooltip is showing only the sums, which is expected, but i dont know how to proceed. If there's another way that passes over the hover, like clicking and, in the click, I recognize the series and all those that are there, that would help immensely too
What I Have right now
What I Want
After looking it up, i built the chart this way:
(right now its a code nightmare, but i will methodify everything properly later)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim sql As New SqlCommand(my query, my connector)
connect()
Dim rs = sql.ExecuteReader
Dim dtTest1 As DataTable = New DataTable
If rs.Read Then
dtTest1.Load(rs)
End If
disconnect()
Dim arrayTempoCallback(dtTest1.Rows.Count) As Double
Dim arrayTempoInaptidao(dtTest1.Rows.Count) As Double
Dim arrayTempoParabens(dtTest1.Rows.Count) As Double
Dim arrayTempoRecusa(dtTest1.Rows.Count) As Double
Dim arrayTempoSemDados(dtTest1.Rows.Count) As Double
For Each item In dtTest1.Rows
arrayTempoCallback(dtTest1.Rows.IndexOf(item)) = item("TEMPO_CALLBACK")
arrayTempoInaptidao(dtTest1.Rows.IndexOf(item)) = item("TEMPO_INAPTIDAO")
arrayTempoParabens(dtTest1.Rows.IndexOf(item)) = item("TEMPO_PARABENS")
arrayTempoRecusa(dtTest1.Rows.IndexOf(item)) = item("TEMPO_RECUSA")
arrayTempoSemDados(dtTest1.Rows.IndexOf(item)) = item("TEMPO_SEMDADOS")
Next
Dim QuartisCallBack = Quartiles(arrayTempoCallback)
Dim QuartisTempoInaptidao = Quartiles(arrayTempoInaptidao)
Dim QuartisTempoParabens = Quartiles(arrayTempoParabens)
Dim QuartisTempoRecusa = Quartiles(arrayTempoRecusa)
Dim QuartisTempoSemDados = Quartiles(arrayTempoSemDados)
Dim tabelafinal As New DataTable
tabelafinal.Columns.Add("VALORES", GetType(String))
tabelafinal.Columns.Add("COLUNA", GetType(String))
tabelafinal.Columns.Add("COR", GetType(String))
Dim somaS As Integer = 0
Dim somaH As Integer = 0
Dim somaC As Integer = 0
Dim somaD As Integer = 0
For Each linha In dtTest1.Rows
If linha("TEMPO_INAPTIDAO") < QuartisTempoInaptidao.Item1 Then
somaS += 1
ElseIf linha("TEMPO_INAPTIDAO") >= QuartisTempoInaptidao.Item1 AndAlso linha("TEMPO_INAPTIDAO") < QuartisTempoInaptidao.Item2 Then
somaH += 1
ElseIf linha("TEMPO_INAPTIDAO") >= QuartisTempoInaptidao.Item2 AndAlso linha("TEMPO_INAPTIDAO") < QuartisTempoInaptidao.Item3 Then
somaC += 1
ElseIf linha("TEMPO_INAPTIDAO") >= QuartisTempoInaptidao.Item3 Then
somaD += 1
End If
Next
tabelafinal.Rows.Add(somaS, "TM_INAP", "D")
tabelafinal.Rows.Add(somaH, "TM_INAP", "C")
tabelafinal.Rows.Add(somaC, "TM_INAP", "H")
tabelafinal.Rows.Add(somaD, "TM_INAP", "S")
somaC = somaD = somaH = somaS = 0
For Each linha In dtTest1.Rows
If linha("TEMPO_PARABENS") < QuartisTempoParabens.Item1 Then
somaS += 1
ElseIf linha("TEMPO_PARABENS") >= QuartisTempoParabens.Item1 AndAlso linha("TEMPO_PARABENS") < QuartisTempoParabens.Item2 Then
somaH += 1
ElseIf linha("TEMPO_PARABENS") >= QuartisTempoParabens.Item2 AndAlso linha("TEMPO_PARABENS") < QuartisTempoParabens.Item3 Then
somaC += 1
ElseIf linha("TEMPO_PARABENS") >= QuartisTempoParabens.Item3 Then
somaD += 1
End If
Next
tabelafinal.Rows.Add(somaS, "TM_PARABENS", "S")
tabelafinal.Rows.Add(somaH, "TM_PARABENS", "H")
tabelafinal.Rows.Add(somaC, "TM_PARABENS", "C")
tabelafinal.Rows.Add(somaD, "TM_PARABENS", "D")
somaC = somaD = somaH = somaS = 0
For Each linha In dtTest1.Rows
If linha("TEMPO_RECUSA") < QuartisTempoRecusa.Item1 Then
somaS += 1
ElseIf linha("TEMPO_RECUSA") >= QuartisTempoRecusa.Item1 AndAlso linha("TEMPO_PARABENS") < QuartisTempoRecusa.Item2 Then
somaH += 1
ElseIf linha("TEMPO_RECUSA") >= QuartisTempoRecusa.Item2 AndAlso linha("TEMPO_PARABENS") < QuartisTempoRecusa.Item3 Then
somaC += 1
ElseIf linha("TEMPO_RECUSA") >= QuartisTempoRecusa.Item3 Then
somaD += 1
End If
Next
tabelafinal.Rows.Add(somaS, "TM_RECUSA", "S")
tabelafinal.Rows.Add(somaH, "TM_RECUSA", "H")
tabelafinal.Rows.Add(somaC, "TM_RECUSA", "C")
tabelafinal.Rows.Add(somaD, "TM_RECUSA", "D")
somaC = somaD = somaH = somaS = 0
For Each linha In dtTest1.Rows
If linha("TEMPO_SEMDADOS") < QuartisTempoSemDados.Item1 Then
somaS += 1
ElseIf linha("TEMPO_SEMDADOS") >= QuartisTempoSemDados.Item1 AndAlso linha("TEMPO_PARABENS") < QuartisTempoSemDados.Item2 Then
somaH += 1
ElseIf linha("TEMPO_SEMDADOS") >= QuartisTempoSemDados.Item2 AndAlso linha("TEMPO_PARABENS") < QuartisTempoSemDados.Item3 Then
somaC += 1
ElseIf linha("TEMPO_SEMDADOS") >= QuartisTempoSemDados.Item3 Then
somaD += 1
End If
Next
Dim somaFinalSemDadosS As Integer = If(somaS < 0, 0, somaS)
Dim somaFinalSemDadosH As Integer = If(somaH < 0, 0, somaH)
Dim somaFinalSemDadosC As Integer = If(somaC < 0, 0, somaC)
Dim somaFinalSemDadosD As Integer = If(somaD < 0, 0, somaD)
tabelafinal.Rows.Add(somaFinalSemDadosS, "TM_SEMDADOS", "S")
tabelafinal.Rows.Add(somaFinalSemDadosH, "TM_SEMDADOS", "H")
tabelafinal.Rows.Add(somaFinalSemDadosC, "TM_SEMDADOS", "C")
tabelafinal.Rows.Add(somaFinalSemDadosD, "TM_SEMDADOS", "D")
somaC = somaD = somaH = somaS = 0
For Each linha In dtTest1.Rows
If linha("TEMPO_CALLBACK") < QuartisCallBack.Item1 Then
somaS += 1
ElseIf linha("TEMPO_CALLBACK") >= QuartisCallBack.Item1 AndAlso linha("TEMPO_PARABENS") < QuartisCallBack.Item2 Then
somaH += 1
ElseIf linha("TEMPO_CALLBACK") >= QuartisCallBack.Item2 AndAlso linha("TEMPO_PARABENS") < QuartisCallBack.Item3 Then
somaC += 1
ElseIf linha("TEMPO_CALLBACK") >= QuartisCallBack.Item3 Then
somaD += 1
End If
Next
Dim somaFinalCallBackS As Integer = If(somaS < 0, 0, somaS)
Dim somaFinalCallBackH As Integer = If(somaH < 0, 0, somaH)
Dim somaFinalCallBackC As Integer = If(somaC < 0, 0, somaC)
Dim somaFinalCallBackD As Integer = If(somaD < 0, 0, somaD)
tabelafinal.Rows.Add(somaFinalCallBackS, "TEMPO_CALLBACK", "S")
tabelafinal.Rows.Add(somaFinalCallBackH, "TEMPO_CALLBACK", "H")
tabelafinal.Rows.Add(somaFinalCallBackC, "TEMPO_CALLBACK", "C")
tabelafinal.Rows.Add(somaFinalCallBackD, "TEMPO_CALLBACK", "D")
Dim dv As DataView = New DataView(tabelafinal)
Chart1.AlignDataPointsByAxisLabel()
Chart1.DataBindCrossTable(dv, "COR", "COLUNA", "VALORES", "")
For Each cs As Series In Chart1.Series
cs.ChartType = SeriesChartType.StackedColumn100
cs.ToolTip = "Pessoas = #VALY"
Next
End Sub
Friend Function Quartiles(ByVal afVal As Double()) As Tuple(Of Double, Double, Double)
Dim iSize As Integer = afVal.Length
System.Array.Sort(afVal)
Dim iMid As Integer = iSize / 2
Dim fQ1 As Double = 0
Dim fQ2 As Double = 0
Dim fQ3 As Double = 0
If iSize Mod 2 = 0 Then
fQ2 = (afVal(iMid - 1) + afVal(iMid)) / 2
Dim iMidMid As Integer = iMid / 2
If iMid Mod 2 = 0 Then
fQ1 = (afVal(iMidMid - 1) + afVal(iMidMid)) / 2
fQ3 = (afVal(iMid + iMidMid - 1) + afVal(iMid + iMidMid)) / 2
Else
fQ1 = afVal(iMidMid)
fQ3 = afVal(iMidMid + iMid)
End If
ElseIf iSize = 1 Then
fQ1 = afVal(0)
fQ2 = afVal(0)
fQ3 = afVal(0)
Else
fQ2 = afVal(iMid)
If (iSize - 1) Mod 4 = 0 Then
Dim n As Integer = (iSize - 1) / 4
fQ1 = (afVal(n - 1) * 0.25) + (afVal(n) * 0.75)
fQ3 = (afVal(3 * n) * 0.75) + (afVal(3 * n + 1) * 0.25)
ElseIf (iSize - 3) Mod 4 = 0 Then
Dim n As Integer = (iSize - 3) / 4
fQ1 = (afVal(n) * 0.75) + (afVal(n + 1) * 0.25)
fQ3 = (afVal(3 * n + 1) * 0.25) + (afVal(3 * n + 2) * 0.75)
End If
End If
Return New Tuple(Of Double, Double, Double)(fQ1, fQ2, fQ3)
End Function
End Class
If I understand correctly, you are trying to show a tooltip (when hovering the cursor over one colored 'block' in the chart) that shows information about the data that makes up the point.
The problem is that each 'block' is only a single X and Y value. For example: The DataPoint behind the tooltip that shows 'Pessoas = 392' is actually just a simple DataPoint (Series-S X=5 Y=392) with no additional information.
To show a tooltip the way you want each one will have to be pre-set, like this:
point.ToolTip = "User1 in the series\nUser2 in the series\n..."

Count Analysis Number Step -1 to 0 (1/2, 2/3 and 3/3)

how can I make this code work? Let's say we have in Textbox1 - the following: (2,4,6) - the textbox that will be scanned to determine the analysis.
TxtBoxIntDraws.Lines(0) = 3,6,7,9
TxtBoxIntDraws.Lines(1) = 2,6,9,10
TxtBoxIntDraws.Lines(2) = 3,5,7,10
then it will result: >
and if exists count reset to 0, if not exists count +1. the analysis will begin from the last line to the first line. TxtBoxIntDraws.Lines(2) - and finish with TxtBoxIntDraws.Lines(0)
1/3 - 0 (because one value exists 2 or 4 or 6, in TxtBoxIntDraws.Lines(0),
2/3 - 1 (because not not 2/3 - 2,6 or 4,6) and +1 add to value.
3/3 - 3 (because not 3/3 - 2,4,6) and +1 add to value.
so briefly, an analysis, 1/3, 2/3 and 3/3, if the value exists then it will be 0, if the value does not exist, it will be added + 1. It has to start from Step -1 to Line 1. I'm trying to make this code, and I'm not doing worked it.
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Dim lastDraw1 As Integer = 0 '1/3
Dim lastDraw2 As Integer = 0 '2/3
Dim lastDraw3 As Integer = 0 '3/3
For i As Integer = Step to -1 To TxtBoxIntDraws.Lines.Count - 0
Dim lineVals As String() = TxtBoxIntDraws.Lines(i).Split(",")
Select Case lineVals.Count()
Case 1
lastDraw1 = 0
lastDraw2 += 1
lastDraw3 += 1
TextBox1.Text = lastDraw1
TextBox2.Text = lastDraw2
TextBox3.Text = lastDraw3
Case 2
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 += 1
TextBox1.Text = lastDraw1
TextBox2.Text = lastDraw2
TextBox3.Text = lastDraw3
Case 3
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 = 0
TextBox1.Text = lastDraw1
TextBox2.Text = lastDraw2
TextBox3.Text = lastDraw3
Case Else
'This should probably also be handled
End Select
Next
End Sub
Not sure what you are doing but to fix the For...Next
Dim StartIndex = TxtBoxIntDraws.Lines.Length - 1
For i = StartIndex To 0 Step -1
'Your code here
Next

Case Count 0 Digit in VB.Net

Why Don't work Case 0 in my code? In this example, if there is nothing in that line (nothing), it should be +1, but it shows me the result 0.
Dim StartIndex = TextBoz1.Lines.Length - 1
For i = StartIndex To 0 Step -1
Dim lineVals As String() = TextBoz1.Lines(i).Split(",")
Select Case lineVals.Count()
Case 0
lastDraw1 += 1
lastDraw2 += 1
lastDraw3 += 1
Case 1
lastDraw1 = 0
lastDraw2 += 1
lastDraw3 += 1
Case 2
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 += 1
Case 3
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 = 0
End Select
Next
TextBox8.Text = lastDraw1
TextBox9.Text = lastDraw2
TextBox10.Text = lastDraw3
End Sub
It does not equal to zero. Try a small sample.
Dim s As String = ""
Dim v As String() = s.Split(","c)
Console.WriteLine(v.Count) ' Display 1
In your case, you would need to check if the string is empty first.
If String.IsNullOrEmpty(TextBoz1.Lines(i)) Then
lastDraw1 += 1
lastDraw2 += 1
lastDraw3 += 1
Else
Dim lineVals As String() = TextBoz1.Lines(i).Split(",")
Select Case lineVals.Count()
Case 1
lastDraw1 = 0
lastDraw2 += 1
lastDraw3 += 1
Case 2
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 += 1
Case 3
lastDraw1 = 0
lastDraw2 = 0
lastDraw3 = 0
End Select
End If

taking average of 10 sample in vb2010

I have used below code to find average of 10 sample . But during first time it take sample and do the averaging . during next cycle counter not become Zero.and text box not updating
Static counter As Integer = 0
DIm average_sum As Double = 0
If counter < 10 Then
counter = counter + 1
Count_val.Text = counter
Dim array(10) As Double
For value As Integer = 0 To counter
array(counter) = k
average_sum = average_sum + array(counter)
Next
If counter = 10 Then
average_sum = average_sum / array.Count
System.Threading.Thread.Sleep(250)
Array_count.Text = average_sum
End If
If counter > 10 Then
average_sum = 0
counter = 0
End If
End If
If Avg_count < 10 Then
Dim array(10) As Double
For value As Double = 0 To Avg_count
array(Avg_count) = k
average_sum = average_sum + array(Avg_count)
Avg_count = Avg_count + 1
Next
If Avg_count = 10 Then
average_sum = average_sum / Avg_count
System.Threading.Thread.Sleep(250)
Average.Text = average_sum
Avg_count = 0
End If
End If
Here count value setting properly . But after 2 to3 cycle Average will done earlier itself same thing i writen in excel to compare averages but not matching with average and excel sheet data
Below is excel sheet code.Both code are in timer1 block.
If counter < 10 Then
'counter = 0
'average_sum = 0
Dim headerText = ""
Dim csvFile As String = IO.Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not IO.File.Exists((csvFile)) Then
headerText = "Date,TIME ,Current, "
End If
Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
If headerText.Length > 0 Then
outFile.WriteLine(headerText)
End If
Dim date1 As String = "25-10-2014"
Dim time1 As String = TimeOfDay()
Dim x As String = date1 + "," + time1 + "," + distance
outFile.Write(x)
End Using
End If
If counter > 10 Then
counter = 0
End If

InvalidArgument=Value of '2' is not valid for 'index'

Dim group11_0_count = 0
Dim group11_1_count = 0
Dim group11_2_count = 0
Dim m As Integer = 0
Dim n As Integer = 0
Dim increment2 As Integer
For m = 0 To machings2.Items.Count - 1
For n = 0 To 3
If machings2.Items(m).ToString.Chars(n) = "1" Then
increment2 = increment2 + 1
End If
Next
If (increment2 = 0) Then
group11_0_count = group11_0_count + 1
group11_1_0.Items.Add(machings2.Items(m))
End If
If (increment2 = 1) Then
group11_1_count = group1_1_count + 1
group11_1_1.Items.Add(machings2.Items(m))
End If
If (increment2 = 2) Then
group11_2_count = group1_2_count + 1
group11_1_2.Items.Add(machings2.Items(m))
End If
increment2 = 0
Next
If (group11_0_count > 0 AndAlso group11_1_count > 0) Then
Dim result = ""
Dim index As Integer = 0
Dim gg As Integer = 0
Dim hh As Integer = 0
Dim i As Integer = 0
For hh = 0 To group11_1_count - 1
For gg = 0 To group11_0_count - 1
result = ""
index = 0
For i = 0 To 3
If group11_1_0.Items(gg).ToString.Chars(i) <> group11_1_1.Items(hh).ToString.Chars(i) Then
result &= "-"
index = index + 1
Else
result &= group11_1_0.Items(gg).ToString.Chars(i)
End If
Next
If (index = 1) Then
machings3.Items.Add(result)
End If
Next
Next
End If
I am comparing the items of two combobox items like that
combobox1 items
0000
combobox items
0001
0010
the result will be like that in machings3 combobox
000-
00-0
Here the differnce between two items indicated by - sign
But i am getting InvalidArgument=Value of '2' is not valid for 'index'.
I Can't make sense out of your source and where the IndexOutOfRangeException occurs. But you know that you need 3 Items in a Combobox to access Item with Index 2?! Every collection starts with 0.