CMSC 351 - Programming Homework #1 - Due by 11:59pm on March 26th
This will be worth 15 points in the HW/Quiz/Programming pool of points.
Due 11:59pm, Monday March 26th (though I encourage doing it earlier,
I am allowing the use of Spring Break to work on it if you so desire).
We define an algorithm as follows:
it takes a four-digit number as input (value)
it sorts the digits in descending order to get n1
it sorts the digits in ascending order to get n2
it calculates n1 - n2
to get newValue which is also a four-digit number
(pad with leading "0" values to get to a four-digit number if needed)
if the number in newValue
is one that we've seen before, we stop at that point
otherwise, if we haven't gone through all 10,000 possible four-digit
numbers, make value=newValue
and repeat the above
the output is the number of "iterations" it had to go through
to get to the the first duplicate and the value that was the
duplicate (returning an object or structure that contains the
two is likely the easiest approach)
Example iteration 1
value: 3546
n1: 6543
n2: 3456
newValue: 3087
Example iteration 2
value: 3087
n1: 8730
n2: 0378
newValue: 8352
Example iteration 3
value: 8352
n1: 8532
n2: 2358
newValue: 6174
Example iteration 4
value: 6174
n1: 7641
n2: 1467
newValue: 6174
For this programming homework assignment (individual like the other
homework assignments, and "no searching for solutions" like other
closed programming projects),
you will need to implement a program in Java or C that
allows you to programmatically determine (don't look for math tricks,
just brute force the algorithm as stated above) the following:
the smallest number of iterations taken
the largest number of iterations taken
the most popular repeat four-digit number (so if 253 of the inputs
lead to 1357 being the first value to appear twice, and no other
four-digit number appeared more often as the "repeated" number,
then it would be the most popular repeat)
This means that your code should
(a) implement the algorithm, and then
(b) run the algorithm on all inputs from 0000 to 9999 to determine
the answers about smallest/largest number of iterations and the
most-repeated "stopping" value.
You will submit a PDF file on
ELMS
with:
the smallest iterations, the largest iterations, the most popular digit set
your opinion on whether your program could reasonably be used to determine the answer if the input were 5-digits long and if the input was 6-digits wrong (and write a sentence or two explaining your opinion)
your source code
Web Accessibility