Very early in every beginning computer class they cover number systems, right?
Positional number systems with base 10: Each digit can have a value of 0, 1, ... 9
How many decimal numbers are there with three digits? (Answer 10 to the power 3, or 1000);
How about base 2? Surely they covered that. Each binary digit can have a value of 0, 1
How many binary numbers are there with three binary digits? (Fill in the blank here: ___________)
They might not have covered base 3 specifically, but it is an obvious extension: Each ternary digit can have a value of 0, 1, 2
How many numbers with base 3 are there with three ternary digits? (Fill in the blank here: ___________)
Ho-hum, right? I wasn't in your class, but I can see it plainly: All of those eyes rolling upward. I can even read all of those thoughts: "So what? When do we get to the Good Stuff? I hope we get out of class soon. Won't that bell ever ring?"
Well, here's an application (that is very simple to implement) using number systems like those that were covered:
Think of your display as a number. We will display some symbols representing the digits of a number.
If there are three positions, think of it as a three-digit number. If there are two possible symbols then it is a binary number.
First question: How many different patterns are there? (Answer : 2 to the power 3). Surely they covered stuff like this.
Next question: How can I use my vast knowledge of number systems to display all possible patterns?
Well, you can have a counter that goes from 0 through 7. Just display the binary digits of each count. Or, if we wanted symbols, say '@' and '#' just let '@' represent binary 0 and let '#' represent binary 1
I like to visualize the output before writing the program, and my program output might look like the following:
Number of positions = 3
Number of symbols = 2 (@ #)
Number of patterns = 8
N Base 2 Display
-----------------------
0 0 0 0 (@ @ @)
1 0 0 1 (@ @ #)
2 0 1 0 (@ # @)
3 0 1 1 (@ # #)
4 1 0 0 (# @ @)
5 1 0 1 (# @ #)
6 1 1 0 (# # @)
7 1 1 1 (# # #)
After debugging, I would remove superfluous output and just show whatever the assignment requires.
Now, what if you wanted three symbols, '@', '#', '$'
So now think of the number that leads to the display is a number in base 3. (Each digit can have value 0, 1, 2)
The number of different displays is equal to (3 to the power 3) = 27
I would make a counter that goes from zero through 26 and show the base-3 digits for each value and the symbols for those digits:
Then my program output might look like this:
Number of positions = 3
Number of symbols = 3 (@ # $)
Number of patterns = 27
N Base 3 Display
-----------------------
0 0 0 0 (@ @ @)
1 0 0 1 (@ @ #)
2 0 0 2 (@ @ $)
3 0 1 0 (@ # @)
4 0 1 1 (@ # #)
5 0 1 2 (@ # $)
6 0 2 0 (@ $ @)
7 0 2 1 (@ $ #)
8 0 2 2 (@ $ $)
9 1 0 0 (# @ @)
10 1 0 1 (# @ #)
11 1 0 2 (# @ $)
12 1 1 0 (# # @)
13 1 1 1 (# # #)
14 1 1 2 (# # $)
15 1 2 0 (# $ @)
16 1 2 1 (# $ #)
17 1 2 2 (# $ $)
18 2 0 0 ($ @ @)
19 2 0 1 ($ @ #)
20 2 0 2 ($ @ $)
21 2 1 0 ($ # @)
22 2 1 1 ($ # #)
23 2 1 2 ($ # $)
24 2 2 0 ($ $ @)
25 2 2 1 ($ $ #)
26 2 2 2 ($ $ $)
Summary:
The thing can be done for any number of positions and any number of symbols. It will result in a finite number of deterministic operations.
If this approach appeals to you, think of how the program will work:
A single loop for the counter. If there are N positions and S symbols, the loop limit (number of patterns) is
S to the power N
For each value of the counter, derive digits in base S . For each count value you can store the Base-S digits in an array (size N) if you want to. You can use the digits to index into an array of characters (or Strings) that holds the symbols corresponding to the digits. Print out the symbols, whatever...
Then, it's time to start writing code. Implementation is a snap.
Cheers!
Z