Reverse a string of numbers using a while loop (VB) - vb.net

I am fairly new to coding in VB and I am trying to reverse the string of numbers in the variable 'binary' by using a while loop (at the bottom of the code) but when the program runs I get a System.IndexOutOfRangeException error. What changes do I need to make to fix this?
Thanks
Module Module1
Sub Main()
Dim denary As Integer
Dim binary As String = " "
Dim x As Integer
Dim y As Integer = 0
Console.WriteLine("What is the denary number?") 'Asks the user what number denary number they want converted
denary = Console.Read()
While denary > 0 'Calculates the binary number, but reversed
If denary Mod 2 = 0 Then
binary = binary + "0"
denary = denary / 2
Else
binary = binary + "1"
denary = denary / 2
End If
End While
Console.WriteLine("The binary equivalent is:" & binary) 'Prints the binary number in reverse
Console.ReadLine()
x = Len(binary)
While x > 0
Console.WriteLine(binary(x)) 'Print the correct binary equivalent (Not working)
Console.ReadLine()
x = x - 1
End While
End Sub
End Module

Array indexes start at 0, so the last one is always 1 less than the length of the array:
x = Len(binary) - 1
While x >= 0
'...

Related

How do i make the code below check if neighboring numbers have a difference of one?

Module Module1
Dim jumbled As Integer = 0
Sub Main()
Console.WriteLine("enter a number: ")
Dim neighno As String = Console.ReadLine()
Dim Neighncount As Integer = neighno.Count
'This part of the code should go through each letter that the user entered,go to the number after and the number before in each loop convert it to integer and add one then check if this number equals the current C value '
For Each c As String In neighno
If neighno(c).Equals(Convert.ToString(Convert.ToInt32(neighno(c + 1))) - 1) Or neighno(c).Equals(Convert.ToString(Convert.ToInt32(neighno(c + 1))) + 1) Then
jumbled = jumbled + 1
End If
Next
' an error for the index being outside of the array's bounds is also shown whenever the code is run'
If jumbled = neighncount Then
Console.WriteLine("jumbled")
Console.ReadLine()
Else
Console.WriteLine("Not jumbled")
Console.ReadLine()
End If
End Sub
End Module

How can put split integers in a two-dimensional array?

I making matrix calculator. so, Textbox_A contains vbCrLf and tries to put it in Array_A.
and I would like to put Array_A in Result Matrix.
It's like
Textbox_a:
(1 2 3)
(4 5 6)
[Matrix to Array]
Array_a(0)(0) = 1
Array_a(0)(1) = 2
Array_a(0)(2) = 3
Array_a(1)(0) = 4
...
I have done string splits through several articles, but changing them to integers causes many problems.
This picture is Matrix_A and result Matrix
I don't know if the size of your initial matrix, formatted as text, is fixed, but here is some code to help you get started. The code tries to calculate the number of columns and rows.
The actual code is in the TextToArray function, that takes as input as string formatted as you described:
(1 2 3) (cr/lf)
(4 5 6)
and outputs a two dimensional array. The Main sub is just used to call TextToArray and display results.
So, in your example, you should pass TextBox_A.Text to TextToArray
There is minimal error checking here - you should add more to validate that data entered are numbers (check the Integer.TryParse function) and that the number of columns is the same across lines.
Sub Main(args As String())
Dim myInput As String = "(1 2 3)" & vbCrLf & "(4 5 6)"
Dim ret As Integer(,) = TextToArray(myInput)
If ret IsNot Nothing Then
For i As Integer = 0 To ret.GetUpperBound(0) - 1
For n As Integer = 0 To ret.GetUpperBound(1) - 1
Console.WriteLine(i & "," & n & "=" & ret(i, n))
Next
Next
Else
Console.WriteLine("No results - wrong input format")
End If
Console.ReadLine()
End Sub
Private Function TextToArray(matrix As String) As Integer(,)
Dim noOfRows As Integer = matrix.Split(vbCrLf).Count
Dim noOfColumns As Integer = 0
If noOfRows > 0 Then
noOfColumns = matrix.Split(vbCrLf)(0).Split(" ").Count
End If
If noOfColumns > 0 And noOfRows > 0 Then
Dim ret(noOfRows, noOfColumns) As Integer
Dim lines As String() = matrix.Split(vbCrLf)
Dim row As Integer = 0
For Each line As String In lines
Dim col As Integer = 0
line = line.Replace("(", "")
line = line.Replace(")", "")
For Each s As String In line.Split(" ")
ret(row, col) = Integer.Parse(s)
col += 1
Next
row += 1
Next
Return ret
Else
Return Nothing
End If
End Function
This outputs:
0,0=1
0,1=2
0,2=3
1,0=4
1,1=5
1,2=6

logic is correct but output is not

It is a function that converts decimal into binary. It is not converting values to binary correctly. This gives 10101 for 19 instead of 10011. How can it be corrected?
Function Binary(n As Integer)
If n = 0 Or n = 1 Then
Console.Write(n)
Else
Binary(n / 2)
Console.Write(n Mod 2)
End If
End Function
Sub Main()
Dim n As Integer
Console.Write("Enter Number: ")
n = Console.ReadLine()
Console.Write(Binary(n))
Console.ReadKey()
End Sub
This is something that might drive you crazy because of how small it is, but the problem is with which division operator you use.
Doing it with Binary(n / 2) treats the integer as a double and passes 9.5 to Binary, while Binary(n \ 2) is the designated integer division operator. You can read more about Arithmetic Operators on Microsoft's website.
Here's what I ran:
Module Module1
Function Binary(n As Integer)
If n = 0 Or n = 1 Then
Console.Write(n)
Else
Binary(n \ 2)
Console.Write(n Mod 2)
End If
End Function
Sub Main()
Dim n As Integer
Console.Write("Enter Number: ")
n = Console.ReadLine()
Console.Write(Binary(n))
Console.ReadKey()
End Sub
End Module
Output for 19: 10011

how to convert (3 digit) Decimal to Ascii from textbox.text?

when i inpot Decimal numbers to textbox, the output will be one word
EX:
input:
textbox.text = 11311711511597105
output:
textbox.text = qussai
You should show us what you had tried.
The full code should be like this:
Module VBModule
Sub Main()
Dim output As String = DecimalToASCII("113117115115097105")
Console.WriteLine(output)
End Sub
Function DecimalToASCII(ByVal input As String) As String
Dim current As String = ""
Dim temp As Integer = 0
If input.Length Mod 3 <> 0 Then
Return "Wrong Input"
End If
For i As Integer = 0 To input.Length - 1 Step 3
temp = 0
For j As Integer = i To i + 2
temp *= 10
temp += CType(input(j).ToString(), Integer)
Next
current &= Chr(temp).ToString()
Next
Return current
End Function
End Module

VB 2008 (Console) "For Each Loop" Lottery Results Comparison

I am facing an issue to perform numbers comparison between the integers inputted by user and random numbers generated by the codes.
Each of the integers input by the user should then be compared with the LOTTO numbers to check for a match. A For each… loop needs to be used to achieve this.
After checking all the 7 user input integers against the LOTTO numbers, the total number of matches should be output to the user. If there are no matches the output should read “LOOSER!”.
Here are my codes, I'm currently only stuck at the comparison portion, and we need to use for each loop to achieve this.
Imports System
Module Lotto
Sub Main()
'Declaration
Dim numbers(6) As Integer
Dim IsStarted As Boolean = True
'Prompt user to enter
Console.WriteLine("Please enter your 7 lucky numbers from 0 - 9 ONLY")
'Use Do While Loop to re-iterate the prompts
Do While IsStarted
For pos As Integer = 0 To 6
Console.Write("Enter number {0}: ", pos + 1)
'How it stores into an array
numbers(pos) = Console.ReadLine()
'Check if it is a number: use IsNumberic()
If IsNumeric(numbers(pos)) Then 'proceed
'Check if it is NOT 0 < x > 9
If numbers(pos) < 0 Or numbers(pos) > 9 Then
'Don't proceed
Console.WriteLine("Invalid Input")
IsStarted = True
'When any number is invalid, exit the loop
Exit For
End If
End If
IsStarted = False
Next
Loop
'Printing out the array. It can also be written as
'For pos = LBound(numbers) To UBound(numbers)
For pos = 0 To 6
Console.Write(numbers(pos) & " ")
Next
Console.WriteLine()
'Random number generator
Randomize()
Dim random_numbers(6) As Integer
Dim upperbound As Integer = 7
Dim lowerbound As Integer = 0
Dim rnd_number As Double = 0
For pos = 0 To 6
rnd_number = CInt((upperbound - lowerbound) * Rnd() + lowerbound)
random_numbers(pos) = rnd_number
Console.Write(random_numbers(pos) & " ")
Next
'Iterate and compare
Dim isSame As Boolean = False
Dim pos2 As Integer = 0
Dim Counter As Integer = 0
'For check = 0 To 6
'If numbers(pos2).Equals(random_numbers(pos2)) Then
For Each number As Integer In numbers
'Console.WriteLine(pos2 + 1 & ":" & number & ":")
If number.Equals(random_numbers(pos2)) Then
'Console.WriteLine("here is the number that matched:" & number & ":")
isSame = True
pos2 = pos2 + 1
End If
For Each num As Integer In random_numbers
If random_numbers Is numbers Then
Counter = Counter + 1
End If
Next
Next
Console.WriteLine()
'Display result
If isSame = True Then
Console.WriteLine("The total numbers of matches are: " & Counter)
Else
Console.WriteLine("LOOSER!")
End If
Console.ReadLine()
End Sub
End Module
Dim intCursor As Integer = 0
For Each intNumber As Integer In numbers
'Assumes first user chosen number must equal
'the first random number to be considered a match,
'the second user number must equal the second random,
'etc (Ordering is a factor).
If random_numbers(intCursor) = intNumber Then
Counter += 1
End If
intCursor += 1
Next
If (Counter > 0) Then
Console.WriteLine("The total numbers of matches are: " & Counter)
Else
Console.WriteLine("LOOSER!")
End If