Im having problems figuring out how to print in an array - vb.net

Not sure how to output the numbers, once they are in ascending order.
This is the task in the pseudocode that I am trying to move into VB.
Dim a() = {2,3,1,4}
Dim swapped = False
Output the values of a()
Do Swapped 🡨 False
For I = 1 to end of the array Compare a(i-1) with a(i) if they are not in ascending order pass them to swapped (from task 1)
Swapped(a(i-1),a(i)) assign the returned value to the
variable swapped.
While swapped = True
Output the values of a()
Dim num = New Integer() {2, 3, 1, 4}
Dim swapped As Boolean = False
While swapped = False
For i = 1 To 4
If num(i - 1) > num(i) Then
temp = num(i)
num(i) = num(i - 1)
num(i - 1) = temp
swapped = True
Else
swapped = False
End If
Next
End While
While swapped = True
Console.WriteLine(num)
End While
Console.ReadLine()

Here you go:
For Each n As Integer In num
Console.WriteLine(n.ToString)
Next
Btw there's a problem with your swapping algorithm: if they are already in the right you will enter an infinite loop, and if there's a swap on the last number you'll exit the loop even if they are not in the right order. If it puzzles you let me a comment and we'll sort this out.

Although, #laancelot showed you how to output the array, I thought I would show you a simple way to sort an array.
Private Sub OrderArray()
Dim num As Integer() = {2, 3, 1, 4}
Array.Sort(num)
Console.WriteLine("Ascending")
For Each i In num
Console.WriteLine(i.ToString)
Next
'If you want it the other way around
Array.Reverse(num)
Console.WriteLine("Descending")
For Each i In num
Console.WriteLine(i.ToString)
Next
Console.ReadLine()
End Sub

If you don't want to use vb net generic array order method, and you prefer use the swap concept, here you can try:
Private Sub TestOrderSwap()
Dim num = New Integer() {9, 7, 0, 11, 12, 10, 6, 2, 3, 1, 4}
If OrderSwap(num, "ASC") Then
For a = 0 To num.Length - 1
Debug.Print(num(a))
Next
End If
If OrderSwap(num, "DESC") Then
For a = 0 To num.Length - 1
Debug.Print(num(a))
Next
End If
End Sub
Private Function OrderSwap(ByRef myArray As Integer(), OrderType As String) As Boolean
Dim swp As Integer = 0
Dim swpFlg As Boolean = False
For a = 1 To myArray.Length - 1
swp = myArray(a)
For b = 0 To a - 1
If (myArray(b) > swp And OrderType = "ASC") Or (myArray(b) < swp And OrderType = "DESC") Then
For c = a - 1 To b Step -1
myArray(c + 1) = myArray(c)
Next
myArray(b) = swp
swpFlg = True
Exit For
End If
Next
Next
Return swpFlg
End Function

Related

vb.net array.sort() parameters why 1 short?

I'm sorting an array using code like this:
Array.Sort(arr, 0, intEndingPosition, New myIComparer)
I want the sorting to start with index 0 and end with index intEndingPosition. However, the last element arr(intEndingPosition) was left out and did not get sorted. Why?
intEndingPosition is calculated beforehand like this:
Dim StringOfConcern As String
Dim OneChar(65534), FrqOne(65534) As String
Dim CntNewOnes, CntRptOnes As Integer
Dim c As Char
Dim i, j As Integer
Dim isNew As Boolean
StringOfConcern = TextBox1.Text
OneChar(0) = CStr(StringOfConcern(0))
FrqOne(0) = 1
i = 0
j = 0
For Each c In StringOfConcern.Substring(1)
isNew = True
For j = 0 To i Step 1
If CStr(c) = OneChar(j) Then
isNew = False
FrqOne(j) += 1
Exit For
End If
Next j
If isNew = True Then
i += 1
OneChar(i) = CStr(c)
FrqOne(i) = 1
End If
Next c
CntNewOnes = i + 1
CntRptOnes = 0
For i = 0 To CntNewOnes - 1 Step 1
If FrqOne(i) > 1 Then CntRptOnes += 1
Next i
The sorting follows here. The code in my original question is only illustrative. The actual sorting is:
Array.Sort(FrqOne, OneChar, 0, CntNewOnes - 1)
Array.Reverse(FrqOne, 0, CntNewOnes - 1)
Array.Reverse(OneChar, 0, CntNewOnes - 1)
Note the method declaration for Array.Sort
Public Shared Sub Sort (
array As Array,
index As Integer,
length As Integer,
comparer As IComparer
)
The third parameter is the number of elements in the range to sort (length) not the end index as you suggest.
So let's assume for a minute that your intEndingPosition is 4. This means you're expecting to sort 5 elements i.e. elements at indices 0, 1, 2, 3, 4. However, the number 4 is the length and not the end index thus you're only sorting elements at indices 0, 1, 2, 3.
This explains why you're observing that the elements being sorted is one shorter than you expected.
Put it simply the third parameter should specify the length of elements to sort and not the end index.
Another Example:
Consider the Substring method of the String class:
Public Function Substring (
startIndex As Integer,
length As Integer
) As String
Then assume we have this piece of code:
Dim temp As String = "testing"
Dim result As String = temp.Substring(0, 4)
result is now a string containing 4 characters as 4 in the Substring call indicates the length that should be retrieved as opposed to the end index.
Had 4 been the end index then you'd expect result to contain 5 characters.

Simple sort algortihm not working - Index issues

I am trying to sort an array using an algorithm, but I am having trouble with my index.
Sub Main()
Dim List() As Integer = {9, 8, 5, 6}
Dim InnerPointer As Integer = 0
Dim CurrentValue As Integer
For OutPointer = 1 To List.Length - 1
CurrentValue = List(OutPointer)
While InnerPointer > 0 And List(InnerPointer) > CurrentValue
List(InnerPointer + 1) = List(InnerPointer)
InnerPointer = InnerPointer - 1
End While
List(InnerPointer + 1) = CurrentValue
Next
For Each element In List
Console.WriteLine(element)
Next
Console.ReadKey()
End Sub
I define InnerPointer as 0 because my array is 0 index based, but this means my While Loop wont kick in...and yet I do want to compare index 0 with index 1 to determine which number is bigger. I cant for the life of me work out where the logical fallacy is

VB.net: Error with mid function

Im making a program that will only accept a userID which is in a format like Hal456, the first one is in uppercase, next two lowercase and the last three numbers.
I tried going through the program step by step and it fails at the second IF statement.
Sub Main()
Dim userid As String
Dim flag As Boolean
flag = False
Console.WriteLine("Input userID to check format")
userid = "Hal123"
Dim a, b, c, d, e, f As Integer
a = Asc("a")
b = Asc("z")
c = Asc("A")
d = Asc("Z")
e = Asc("0")
f = Asc("9")
If Len(userid) = 6 Then
If Asc(Left(userid, 1)) > c And Asc(Left(userid, 1)) < d Then
If Asc(Mid(userid, 2, 1)) > a And Asc(Mid(userid, 2, 1)) < b Then
If Asc(Mid(userid, 3, 1)) > a And Asc(Mid(userid, 3, 1)) < b Then
If Asc(Mid(userid, 4, 1)) > e And Asc(Mid(userid, 4, 1)) < f Then
If Asc(Mid(userid, 5, 1)) > e And Asc(Mid(userid, 5, 1)) < f Then
If Asc(Mid(userid, 6, 1)) > e And Asc(Mid(userid, 6, 1)) < f Then
flag = True
Console.WriteLine("Format of userID is correct")
End If
End If
End If
End If
End If
End If
End If
If flag = False Then Console.WriteLine("Format is not correct")
Console.ReadLine()
End Sub
I see the problem with the code. It is a simple fix. Just change all the "<" to "<=" and all the ">" to ">=" without quotes.
Reason:
Your Program will not allow "a" and "z" and "0" and "9" to be part of the userid.
Changing it to greater than or equal to solves this.
Hope this helps!
Try this using Regular Expressions:
Sub Main()
Dim userid As String
Dim flag As Boolean
Console.WriteLine("Input userID to check format")
userid = "Hal123"
flag = CheckUserID(userid)
If flag = False Then Console.WriteLine("Format is not correct")
Console.ReadLine()
End Sub
Private Function CheckUserID(userId As String) As Boolean
Dim regex As Regex = New Regex("([A-Z]){1}([a-z]){2}([0-9]){3}")
Dim match As Match = regex.Match(userId)
Return match.Success
End Function
How about using REGEXP for solving this? I think that should be quite straight Forward.
Something like:
string pattern = #"[A-Z][a-z]{2}\d{3}";
Regex rx = new Regex(pattern);
string uid = "";
do
{
Console.WriteLine("Please insert userID:");
uid = Console.ReadLine();
bool check = rx.IsMatch(uid);
} while (!rx.IsMatch(uid));

array without any duplicate value

the code to generate no. of arrays from one is working..I'm try to make some change to it like below
Function myarray(ByVal arra1() As Integer, ByVal arran() As Integer, ByVal arrNumber As Integer) As Integer()
arran = arra1.Clone()
For i As Integer = 0 To arra1.Length - 1
If i = (arrNumber - 1) Then ' IF arrNumber is 1 then +1 to index 0, If it is 2 then +1 to index 1
arran(i) = arra1(i) + 1
'If there are two duplicate value make on of them zero at a time
For k = 0 To arran.Length - 1
For j = k + 1 To arran.Length - 1
If arran(k) = arran(j) Then
arran(k) = 0
End If
'make any value great than 11 zero
If arran(i) > 11 Then
arran(i) = 0
End If
Next
Next
Else
arran(i) = arra1(i)
End If
Next
'Print the array
For i = 0 To arran.Length - 1
Console.Write(arran(i) & " ")
Next
Console.WriteLine()
Return arran
End Function
what I really need is to decompose for example {1,4,5,5} to be {1,4,0,5} and then {1,4,5,0} the above code generate only {1,4,0,5}
I haven't tested this, but I believe the following code will do what you want. Based on your comments, I've changed the function to return all resulting arrays as an array of arrays, rather than requiring the index to change as an input and returning one array. I also ignored matches of 0, as the conditions you describe don't seem designed to handle them. Because of it's recursion, I think this approach will successfully handle input such as {3, 3, 3, 3}.
Public Function jaggedArray(ByVal inputArray() As Integer) As Integer()()
If inputArray Is Nothing Then
Return Nothing
Else
Dim resultArrays()(), i, j As Integer
Dim arrayMax As Integer = inputArray.GetUpperBound(0)
If arrayMax = 0 Then 'prevents errors later if only one number passed
ReDim resultArrays(0)
If inputArray(0) > 11 Then
resultArrays(0) = {1}
ElseIf inputArray(0) = 11 Then
resultArrays(0) = {0}
Else
resultArrays(0) = {inputArray(0) + 1}
End If
Return resultArrays
End If
For i = 0 To arrayMax
Dim tempArray() As Integer = inputArray.Clone
For j = 0 To arrayMax
If tempArray(j) > 11 Then
tempArray(j) = 0
End If
Next
If tempArray(i) = 11 Then
tempArray(i) = 0
Else
tempArray(i) += 1
End If
splitArray(resultArrays, tempArray)
Next
Return resultArrays
End If
End Function
Private Sub splitArray(ByRef arrayList()() As Integer, ByVal sourceArray() As Integer)
Dim x, y As Integer 'positions of matching numbers
If isValid(sourceArray, x, y) Then
If arrayList Is Nothing Then
ReDim arrayList(0)
Else
ReDim Preserve arrayList(arrayList.Length)
End If
arrayList(arrayList.GetUpperBound(0)) = sourceArray
Else
Dim xArray(), yArray() As Integer
xArray = sourceArray.Clone
xArray(x) = 0
splitArray(arrayList, xArray)
yArray = sourceArray.Clone
yArray(y) = 0
splitArray(arrayList, yArray)
End If
End Sub
Private Function isValid(ByRef testArray() As Integer, ByRef match1 As Integer, ByRef match2 As Integer) As Boolean
For i As Integer = 0 To testArray.GetUpperBound(0) - 1
If testArray(i) > 11 Then
testArray(i) = 0
End If
For j As Integer = i + 1 To testArray.GetUpperBound(0)
If testArray(j) > 11 Then
testArray(j) = 0
End If
If testArray(i) = testArray(j) AndAlso testArray(i) > 0 Then 'added second test to prevent infinite recursion
match1 = i
match2 = j
Return False
End If
Next
Next
match1 = -1
match2 = -1
Return True
End Function

Reducing repetition when accessing an array in VB.NET

for i = 0 to array.length
if array(i) = 2 and array (i+1) = 3 and .. and .. .. then
do xx on array (i+20)
..
..
end if
next
i need to check an array for a specific combination of numbers before i perform an operation, and i need to know the start point of the array as well. does anybody know how i can remove repetition because it's kind of hard to read when you have so many conditions?
array will be typically something like 02 0101 0000 so i need to check 10 consecutive values before i perform any operation
Dim j As Integer = 0
Dim b() = {1, 1}
Dim tempFlg As Boolean = False
Dim a() As Integer = {0, 2, 0, 1, 0, 1, 0, 0, 0, 0}
For i As Integer = 0 To a.Length - 1
If a(i) = b(0) Then
For j = 1 To b.Length - 1
If a(i + j) = b(j) Then
tempFlg = True
End If
Next
End If
Next
If tempFlg = True Then
MsgBox("Item present")
End If