%-------------------------------------% % Homework 5: Max consecutive 1s % % Date: 10/21/2002 % % % % Gun Srijuntongsiri % % gs61 % % ?????? % %-------------------------------------% % Variables: % max1s = largest number of consecutive 1s found so far % currLength = current number of successively generated 1s % numBits = number of bits generated so far % totalBits = total number of bits to generate % str = string of all bits generated numBytes = floor(rand*3+1); % randomly generate # of bytes, b/w 1 and 3. totalBits = 8*numBytes; % total # of bits = 8 * number of bytes % initialize... max1s = 0; currLength = 0; str = ''; % For each bit.. for numBits = 1:totalBits currBit = floor(rand*2); % generate current bit. str = [str, num2str(currBit)]; if currBit==1 % If it's a 1, currLength = currLength+1; % increment number of successively generated 1s if currLength > max1s % If the current length is greater than max1s, max1s = currLength; % then make it the max.. end else % If it's a 0, currLength = 0; % reset current length of consecutive 1s. end end disp(['Generated ', num2str(numBytes),' bytes worth of bits.']); disp(['The bit sequence is ', str, '.']); disp(['The maximum number of successively generated 1s is ', num2str(max1s), '.']);