97.9k views
1 vote
Write a function that iteratively appends random 1's and 0's to an array to form a binary number. The function returns the binary result when it's decimal equivalent is larger than the input number. [outArray] = potd11(inputNumber)

1 Answer

0 votes

Answer:

function [result] = potd11(userInput)

result = [0];

d = 0;

while d < userInput

result = [result, randi([0,1])];

d = sum(result .* (2.* ones(1, numel(result))).^(numel(result)-1 :-1:0));

end

end

Step-by-step explanation:

  • Initialize the variables from 0 as a starting point.
  • Use a while to run it until d is less than userInput variable.
  • Add random 1's and 0's to the array.
  • Return the binary result when it's decimal equivalent is larger than the input number.
User Lee McAlilly
by
4.8k points