I'm a TA for an Introduction to MATLAB course, and the class has not yet learned the use of arrays (or in MATLAB, vectors). There is an exam coming up and one of the questions on the study guide is as follows:
[Tough problem] A mode is the number in a sequence that appears the most number of times. A user is prompted to enter a sequence of non-negative numbers in non-decreasing order one-by-one. The user indicates the end of the sequence by entering a negative number. Write a script to obtain such user input and determine the mode of the sequence. If there are multiple modes, you may report any one of them as the mode. Do not use arrays. Below is an example run:
Determine mode of a set of nonnegative integers.
Use a negative number to quit.
Give me a number: 70
Another number not smaller than the previous: 71
Another number not smaller than the previous: 80
Another number not smaller than the previous: 80
Another number not smaller than the previous: 80
Another number not smaller than the previous: 91
Another number not smaller than the previous: 93
Another number not smaller than the previous: -1
Mode is 80.
I've been thinking about it, but I can't come up with a good solution. Does anyone know if there is a good way to solve this?
The only solutions I can come up with are ugly hacks that attempt to simulate the use of an array by other means, such as using a string with delimiters to simulate a dictionary-like object.