Calculating Binary In this assignment, you are asked to solve a problem using 3
ID: 3883505 • Letter: C
Question
Calculating Binary In this assignment, you are asked to solve a problem using 3 different programming languages: Python (33.33%), Ada (33.33%), and VB (33.33%). You are required to find a compiler for each of the mentioned languages and test them on your own machines The Problem Given a binary String as input, composed of O's and I's, give the decimal value for the input. Algorithm Given a binary string, b, of size, n, indexed 0,1,2,.,n-1. Denote character's integer value of b as b, such that I = 0, 1, 2, , n-1. The decimal value, d = 4-1 × 2° + bn-2 × 21 + + bo × 2"-1 Sample Output 120013 EEFoL Sot a binary 00w2 Ezzaz: Set binary O0AS EEEOEE Not binary Enter a binary Enter binary 10927 Other Requirements .As shown in the sample output, a validation must be performed to check if the String is binary .Add comments to your code to help make it more understandable .Add a comment at the top of the code that includes your name(s), date, and assignment number. Note: names(s) in case you worked on your assignments in groups of two's.Explanation / Answer
Imports System
Public Module Module1
Function Reverse(ByVal value As String) As String
' Convert to char array.
Dim arr() As Char = value.ToCharArray()
' Use Array.Reverse function.
Array.Reverse(arr)
' Construct new string.
Return New String(arr)
End Function
Function Bin2Dec(BinaryString As String) As Integer
Dim revBinStr As String = Reverse(BinaryString)
Dim pwrOf2 As Integer = 0
Dim ans As Long = 0L
For Each n As Char In revBinStr
Select Case n
Case Is = "0"c
Case Is = "1"c
ans += 1L << pwrOf2
Case Else
'error - input not in binary, only 1's and 0's
Stop
End Select
pwrOf2 += 1
Next
Return ans
End Function
Public Sub Main()
Dim nl As String = Environment.NewLine
Console.WriteLine("Enter a binary: " + nl)
Dim input = Console.ReadLine()
If Regex.IsMatch(input, "^[01]+$") Then
Console.WriteLine(Bin2Dec(input))
Else
Console.WriteLine("Error: Not a binary")
END IF
End Sub
End Module
Code link: https://paste.ee/p/HLSri
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.