Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions lib/binary_to_decimal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,33 @@
# The array is randomly filled with 0’s and 1’s.
# The most significant bit is at index 0.
# The least significant bit is at index 7.
# Calculate and return the decimal value for this binary number using
# the algorithm you devised in class.
def binary_to_decimal(binary_array)
raise NotImplementedError
# Calculate and return the decimal value for this binary number using the algorithm you devised in class.
def binary_to_decimal
puts "This has not been implemented" # makes array of 8 random elements of 0's or 1's
binary_array = []
8.times do
binary_array << Random.rand(2)
end

# here we are creating a string representation of the binary number
binary_string = ""
binary_array.each do |num|
binary_string += "#{num}"
end
print "The binary number is " + binary_string

decimal_sum = 0 # decimal_sum will be equivalent to final decimal value for the binary number
e = 0
i = -1 # starting at the last element of the array (index[-1])

binary_array.length.times do
if binary_array[i] == 1
decimal_sum += 2**e # if the value is equal to 1, then 2^e (starting at 0) is added to the decimal_sum
end
i -= 1
e += 1
end
print "\n the decimal value is " + decimal_sum.to_s + "\n"
end

puts "Calling binary to decimal"