Discussion Forum

Problem 1 in exercise 2

Problem 1 in exercise 2

by Robert Rekefors -
Number of replies: 3

The task in problem 1 of exercise 2 is to provide a pseudocode for converting positive integers into binary numbers, numers with base 2 instead of 10 where for example 1=1, 2=10, 3=11 and 4=100. The instructions say that they should be stored as arrays, how does that work? Shouldn't the return value be just a binary number that for example converts 5 to 101 and 6 to 110? 363=3*10^2+6*10^1+3*10^0. For binary numbers, we instead have 1111=1*2^3+1*2^2+1*2^1+1*2^0. I wonder if converting base 10 to base 2 should be via the logarithm or division with 2 and then some procedures or modulo 2 even though that only gives the last digit of the binary number. The python function np.log has the base e. Calculators only have log for base 10 and ln for base e. 

In reply to Robert Rekefors

Sv: Problem 1 in exercise 2

by Marc Hellmuth -
a binary number 101 can be stored in an array, by initializing an array A of size 3 and store in A[1]=1, A[2]=0, A[3]=1 (this requires of course pre-knowledge of the size needed to initialize the array A).

The "how" this works is exactly the task of the exercise. this task does not require log_b computations.
In reply to Marc Hellmuth

Re: Sv: Problem 1 in exercise 2

by Kyle Aidan Tenn -
On slide 8 it says addition, subtraction, multiplication, division, floor, ceiling, modulo,. . . but with the dot dot dot, and with division and floor stated as examples, does this imply integer division is also allowed? or should one use division and then floor?
 
In addition, it states one should use arrays. However, for the first iteration since the array is blank, can we initialize a zero length array(this point I am unclear on??). In other words, it okay to init a zero length array?
In reply to Kyle Aidan Tenn

Sv: Re: Sv: Problem 1 in exercise 2

by Marc Hellmuth -

Dot dot dot = we could have defined more but haven't. Hence use division with floor or ceil.

I am not sure if you can handle a non dynamic array of size 0 to store the respective values. Instead the array should be initialized on the  size needed.