First Lowest Density Array In a multi-dimensional array - vb.net

I'm trying to find the lowest density in a multi-dimensional array.
Say a array looks like this
1 1 1
1 1 <- Lowest density is this one row:1 column:2.
1 1 1
1 1
1 1 1
1 <- Lowest density is this one row 2: column:1.
Here is my broken code
Public ProcessedBits()() As Byte
'Lowest Density detector to find out which values have the highest priority.
Dim nextLowestDensityColumn As Integer = Integer.MaxValue
Dim nextLowestDensityRow As Integer = Integer.MaxValue
For Row = 0 To Uniques.Length - 1
For Column = 0 To Uniques(Row).Length - 1 'check each column in current row.
If ProcessedBits(Row)(Column) = 1 Then 'if the column is processed, then skip it and process the next one.
If Column < nextLowestDensityColumn AndAlso Row < nextLowestDensityRow Then
nextLowestDensityColumn = Column + 1
nextLowestDensityRow = Row
End If
End If
Next
Next
If nextLowestDensityRow <> Integer.MaxValue AndAlso nextLowestDensityColumn <> Integer.MaxValue Then
Row = nextLowestDensityRow
End If
Semi fixed code
'Lowest Density detector to find out which values have the highest priority.
Dim nextLowestDensityColumn As Integer = Integer.MaxValue
Dim nextLowestDensityRow As Integer = Integer.MaxValue
For Row = 0 To Uniques.Length - 1
For Column = 0 To Uniques(Row).Length - 1 'check each column in current row.
If ProcessedBits(Row)(Column) = 1 AndAlso Uniques(Row).Length <> Column + 1 AndAlso ProcessedBits(Row)(Column + 1) = 0 Then 'if the column is processed, then skip it and process the next one.
If (Column + 1) < nextLowestDensityColumn Then
nextLowestDensityColumn = Column + 1
nextLowestDensityRow = Row
End If
End If
Next
Next
If nextLowestDensityRow <> Integer.MaxValue AndAlso nextLowestDensityColumn <> Integer.MaxValue Then
Row = nextLowestDensityRow
End If

Fixed it
'Lowest Density detector to find out which values have the highest priority.
Dim nextLowestDensityColumn As Integer = Integer.MaxValue
Dim nextLowestDensityRow As Integer = Integer.MaxValue
For Row = 0 To Uniques.Length - 1
For Column = 0 To Uniques(Row).Length - 1 'check each column in current row.
If ProcessedBits(Row)(Column) = 1 AndAlso Uniques(Row).Length <> Column + 1 AndAlso ProcessedBits(Row)(Column + 1) = 0 Then 'if the column is processed, then skip it and process the next one.
If (Column + 1) < nextLowestDensityColumn Then
nextLowestDensityColumn = Column + 1
nextLowestDensityRow = Row
End If
End If
Next
Next
If nextLowestDensityRow <> Integer.MaxValue AndAlso nextLowestDensityColumn <> Integer.MaxValue Then
Row = nextLowestDensityRow
End If

Related

How to solve the sum of values in a column in a datagridview in vb.net

Dim Sum As Integer = 0
For i = 1 To DGV.Rows.Count + 1
Sum += DGV.Rows(i).Cells(3).Value
Next
Label7.Text = Sum.ToString()
Result:
This works well for me.
Going through every row and exclude new rows.
Dim SResult As Integer = 0
For Each SF As DataGridViewRow In DGV.Rows
If SF.IsNewRow = False Then
SResult += SF.Cells(3).Value
End If
Next
Label7.Text = SResult

I want to delete a row from InDatagridview that contains textbox.tex and transfer it to OutDatagridview

I want to delete a row from InDatagridview that contains textbox.tex and transfer it to OutDatagridview
if i change the value of textbox it will search to InDatagridview and delete a row from InDatagridview that contains textbox.tex and transfer it to OutDatagridview.
If i change again the textbox value it will search to OutDatagridview and delete a row from OutDatagridview that contains textbox.text
and transfer it to InDatagridview.
Thanks!!!! a lot
Dim found, x, z As Boolean
Dim lookfor As String = Student_numberTextBox.Text
For dgv As Integer = In_TableDataGridView.Rows.Count - 1 To 0 Step -1
If In_TableDataGridView.RowCount > 0 Then
If lookfor = In_TableDataGridView.Rows(dgv).Cells(0).Value.ToString Then
found = True
End If
Else
found = False
End If
Next
If found = False Then
'In_TableBindingSource.AddNew()
If Out_tableDataGridView.RowCount > 0 Then
x = True
Else
x = False
End If
If x = True Then
Dim textout As String = Student_numberTextBox.Text
For inrow As Integer = Out_tableDataGridView.Rows.Count - 1 To 0 Step -1
If textout = Out_tableDataGridView.Rows(inrow).Cells(0).Value.ToString Then
Dim row2 As DataGridViewRow
row2 = Out_tableDataGridView.Rows(inrow)
Out_tableDataGridView.Rows.Remove(row2)
Else
'Exit For
End If
Next
End If
End If
If found = True Then
'Out_tableBindingSource.AddNew()
If In_TableDataGridView.RowCount > 0 Then
z = True
Else
z = False
End If
If z = True Then
Dim textin As String = Student_numberTextBox.Text
For outrow As Integer = In_TableDataGridView.Rows.Count - 1 To 0 Step -1
If textin = In_TableDataGridView.Rows(outrow).Cells(0).Value.ToString Then
Dim row1 As DataGridViewRow
row1 = In_TableDataGridView.Rows(outrow)
In_TableDataGridView.Rows.Remove(row1)
Else
'Exit For
End If
Next
End If`enter code here`
End If
End If

Printing prime numbers

I have to print the sequential 25 prime numbers after I ask the user to input a number. The code below indeed does print prime numbers but it prints empty spaces. I can't figure out how to make it print ONLY the prime numbers and no empty spaces. I have this thus far:
Public Function IsPrime(value)
Dim remainder As Double
Dim rounded As Long
Dim Max As Long
Dim j As Long
IsPrime = 0
Max = 1 + Int(value / 2)
For j = 2 To Max
rounded = Int(value / j)
remainder = (value / j) - rounded
If remainder = 0 Then
Exit For
End If
Next j
If j = Max + 1 Or value = 2 Then
IsPrime = 1
End If
If value <= 1 Then
IsPrime = 0
End If
End Function
Public Sub printprime()
Dim x As Integer
Dim row As Integer
Dim col As Integer
Dim j As Integer
Dim matrix1(1 To 5, 1 To 5) As Integer
x = InputBox("Please enter a number ")
j = 1
For row = 1 To 5
For col = 1 To 5
If IsPrime(x + j) = 1 Then
Cells(row, col) = x + j
matrix1(row, col) = Cells(row, col)
End If
j = j + 1
Next col
Next row
You just need to keep going and only record the primes:
Public Sub printprime()
Dim x As Long
Dim row As Long
Dim col As Long
Dim j As Long
x = InputBox("Please enter a number ")
row = 1
col = 1
For j = x + 1 To 9999
If IsPrime(j) Then
Cells(row, col) = j
col = col + 1
If col = 6 Then
col = 1
row = row + 1
End If
If row = 7 Then Exit Sub
End If
Next j
End Sub
Here's my version
Sub print_prime()
Dim i As Integer
Dim j As Integer
Dim if_prime As Boolean
Dim Count
Count = InputBox("Enter Max Number")
For i = 2 To Count
if_prime = True
If i = 2 Then GoTo nextItem
For j = 2 To i - 1
If i Mod j = 0 Then
if_prime = False
GoTo nextItem
End If
Next j
nextItem:
If if_prime Then Debug.Print i
Next i
End Sub

Select limited data row from filtered data

I have 70,000 rows of data in an Excel sheet. After applying a filter, the total number of visible rows becomes 40,000. Now I would like to select and copy the first 15,000 visible rows only.
This should work for you. I would suggest setting it up to run on a keyboard shortcut.
Const limit As Integer = 15000
Sub GrabFiltered()
Dim r As Range
Dim lr As Range
Dim tr As Range
Dim rr As Range
Dim br As Range
Dim table As Range
Dim rows As Integer
Dim i As Integer
Dim ct As Integer
Dim offset As Integer
Set r = Selection.Cells(1, 1)
If r.End(xlToLeft).Cells(1, 1).FormulaR1C1 <> "" Then
Set lr = r.End(xlToLeft).Cells(1, 1)
Else
Set lr = r
End If
If lr.End(xlUp).Cells(1, 1).FormulaR1C1 <> "" Then
Set tr = lr.End(xlUp).Cells(1, 1)
Else
Set tr = lr
End If
If r.End(xlToRight).Cells(1, 1).FormulaR1C1 <> "" Then
Set rr = r.End(xlToRight).Cells(1, 1)
Else
Set rr = r
End If
rr.Select
If rr.End(xlDown).Cells(1, 1).FormulaR1C1 <> "" Then
Set br = rr.End(xlDown).Cells(1, 1)
Else
Set br = r
End If
Set table = Range(tr, br)
'count the number of rows that are visible
rows = 0
For i = 1 To table.rows.Count
If table.Cells(i, 1).Height <> 0 Then
rows = rows + 1
End If
Next
'limit the number of rows to copy
If rows > limit Then
offset = rows - limit
i = 1
ct = 1
While i <> offset
If br.offset(-ct, 0).Height <> 0 Then
i = i + 1
End If
ct = ct + 1
Wend
Set br = br.offset(-ct, 0)
Set table = Range(tr, br)
End If
table.Copy
End Sub

Trying to delete entire row in datagridview if all cells in row have same value

I have a datagridview in which I populate via a loop and then use the cell painting went to make all cells that have te value " $0.00". What I'm trying to do now is after my populate loop executes I want to go through each row and if each cell contains " $0.00" I want to delete the entire row (row header included). How could I do this with a loop? I was looking around on the internet and came across the "Datagridview1.rows.remove(datagridiew1[row])". Could that be implemented to help me accomplish this? If so, how? Example code would be appreciated. Thank you!
*Edited to include code*
I have the two Subs in which I call the check row right after the populate datagridview sub
Sub PopulateDataGridView()
pb.Value = 0
pb.Visible = True
pb.Enabled = True
'Loop through each column
Dim cIndex As Integer = 0
Dim rIndex As Integer = 0
While cIndex < DataGridView1.ColumnCount
'Loop through and populate each row in column
rIndex = 0
While rIndex < DataGridView1.RowCount
'pb.Value = pb.Value + 1
If cIndex = 0 Then
'Set row header titles
DataGridView1.Rows.Item(rIndex).HeaderCell.Value = sheet.Range("A1").Offset(rIndex + 1, cIndex).Value()
DataGridView1.Rows(rIndex).Cells(cIndex).Value = sheet.Range("A1").Offset(rIndex + 1, cIndex + 1).Value()
End If
If cIndex > 0 Then
DataGridView1.Rows(rIndex).Cells(cIndex).Value = sheet.Range("A1").Offset(rIndex + 1, cIndex + 1).Value()
End If
'Set column header title
DataGridView1.Columns(cIndex).HeaderText = sheet.Range("A1").Offset(0, cIndex + 1).Value
'Change last cell (Result) color Red or Green to represent positive gain or negative loss
If rIndex = RowCount - 2 Then
If DataGridView1.Rows(rIndex).Cells(cIndex).Value < 0 Then
DataGridView1.Item(cIndex, rIndex).Style.BackColor = Color.Red
DataGridView1.Item(cIndex, rIndex).Style.ForeColor = Color.White
End If
If DataGridView1.Rows(rIndex).Cells(cIndex).Value > 0 Then
DataGridView1.Item(cIndex, rIndex).Style.BackColor = Color.Green
DataGridView1.Item(cIndex, rIndex).Style.ForeColor = Color.White
End If
If DataGridView1.Rows(rIndex).Cells(cIndex).Value = 0 Then
DataGridView1.Rows(rIndex).Cells(cIndex).Value = "Broke Even"
End If
End If
pb.Value = pb.Value + 1
rIndex = rIndex + 1
End While
'Make column unsortable
DataGridView1.Columns(cIndex).SortMode = DataGridViewColumnSortMode.NotSortable
cIndex = cIndex + 1
End While
pb.Visible = False
pb.Value = 0
pb.Enabled = False
DataGridView1.AutoResizeColumns()
'Resize all Row Headers so user can see Row Titles without resizing
DataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders)
End Sub
Sub EmptyRowCheck()
Dim SkipRemove As Boolean
'loop through rows in datagrid
For Each Row As DataGridViewRow In DataGridView1.Rows
SkipRemove = False
'loop through each cell in row
For Each Cell As DataGridViewCell In Row.Cells
'if value is not $0.00 then set boolean and exit inner loop
If Not Cell.Value = " $0.00" Then
SkipRemove = True
Exit For
End If
Next
'check if to remove the row or not
If Not SkipRemove = True Then
DataGridView1.Rows.Remove(Row)
End If
Next
End Sub
My code would include
PopulateDataGridView()
EmptyRowCheck()
The issue I'm running into now is with this method it's skipping over every other empty row, only deleting half of the empty rows.
something like this could work:
Dim SkipRemove As Boolean
Dim Rowindex As Integer
'loop through rows in datagrid starting from the bottom
For Rowindex = DataGridView1.Rows.Count - 1 To 0 Step -1
SkipRemove = False
'loop through each cell in row
For Each Cell As DataGridViewCell In DataGridView1.Rows(Rowindex).Cells
If Not Cell.Value = "£0.00" Then
SkipRemove = True
Exit For
End If
Next
'check if to remove the row or not
If Not SkipRemove = True Then
DataGridView1.Rows.RemoveAt(Rowindex)
End If
Next
this starts at the bottom of the datagridview and works up preventing the skipping issue.
It should look something like this:
Dim RemoveThis AS Boolean = True
FOR EACH dgvrow AS Datagridviewrow IN Datagridview1.Rows
'Loop thru each row
FOR i AS Integer = 0 to Datagridview1.Columncount
'Loop thru each column/field of the current row
IF NOT dgvrow(i)="$0.00" THEN RemoveThis = False
'If any one of the cells does not contain the value "$0.00", do not remove the row
NEXT
If RemoveThis THEN Datagridview1.Rows.Remove(dgvrow)
RemoveThis = True
NEXT