% Example program for computing the mode of a sequence of integers.
% The program has been improved somewhat from the version in lecture.

disp('Determine mode of a set of nonnegative integers.')
disp('Use a negative number to quit.')

previous = -1; % Previous number seen
count = 1; % Count of current number
mode = -1; % Best mode so far
modeCount = 0; % Count for best mode so far
number = input('Give me number. '); % Get initial number
while number >= 0 % Quit when negative number is encountered
if number == previous
count = count + 1;
elseif number > previous
count = 1;
else
disp('Sequence must be nondecreasing');
end
if count > modeCount
mode = number;
modeCount = count;
end
previous = number;
number = input('Give me more number. '); % Get next number
end
if mode == -1
disp('Mode is undefined')
else
fprintf('Mode is %d which occurred %d times.\n', mode, modeCount);
end