Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

do not use any of the built in functions for binary and decimal MATLAB Algorithm

ID: 3701504 • Letter: D

Question

do not use any of the built in functions for binary and decimal

MATLAB

Algorithm 9 Write a function with the header [b] myDec2Bin(d), where d is a positive integer in decimal (base 10), and b is the binary representation of d. The output b must be a row vector of ones and zeros, and the leading term must be a 1 unless the decimal input value is 0 For test cases see book page 130 Hint: use a while loop to find the first 2Ax power that is higher than the decimal value, d This will let you know where to start doing your calculations

Explanation / Answer

Code using while loop -

dec_nr = 10;

i = 1;

q = floor(dec_nr/2);

r = rem(dec_nr, 2);

bin(i) = num2str(r(i));

while 2 <= q

dec_nr = q;

i = i + 1;

q = floor(dec_nr/2);

r = rem(dec_nr, 2);

bin(i) = num2str(r);

end

bin(i + 1) = num2str(q);

bin = fliplr(bin)