Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dionisia-Edges-BinaryAndDecimal #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
22 changes: 20 additions & 2 deletions binary_to_decimal.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
# A method named `binary_to_decimal` that creates an array of size 8.
# The array is randomly filled with 0’s and 1’s.
# Print out the array so that it appears as a binary number.
# Calculate the decimal value for this binary number using the algorithm
# Calculate the decimal value for this binary number using the algorithm
# you devised in class. Print out the decimal value and return this value.
def binary_to_decimal
raise NotImplementedError
count = 8
binary_array = Array.new(count)
count.times do |i|
binary_array[i] = rand(2)
end

puts "The binary number is: "
count.times do |i|
print binary_array[i]
end
puts

decimal = 0
binary_array.reverse!
count.times do |i|
decimal += (2 ** i) * binary_array[i]
end
puts "The decimal equivalent for it is: #{decimal}."
return decimal
end

num = binary_to_decimal()
Expand Down