/BinaryAndDecimal

Homework exercises after CS Fun class on Binary and Memory

Primary LanguageRuby

Binary and Decimal

In this assignment you'll be composing two methods based on the description below. Complete the method templates provided in binary_and_decimal.rb file.

Exercises

  1. Write a method named binary_to_decimal that creates an array of size 8. Fill the array randomly 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 you devised in class. Print out the decimal value.
  2. This question has two parts:
    • Devise an algorithm Imagine you've been given two sets of integers. e.g. the first set is [30, 20, 10, 4] and the second set is [20, 10, 4]. Devise an algorithm that comapres the two sets and returns true if the two sets have the same integers and in the same order, and returns false otherwise. e.g. In our example case, for [30, 20, 10, 4] and [20, 10, 4], the two sets do not have all the same integers (30 is the difference) and the order is only partially the same. So, the algorithm should return false.
    • Author a method named array_equals that accepts two integer arrays as parameters (representative of the two sets). The method return true if the arrays contain the same elements in the same order, and returns false otherwise. Note: Do not use Array class methods for comparing the whole array at once. You may use array indexing to retrieve one element at a time, compare individual elements with each other and you may retrieve the length of an array.