Today We Learn About A Counting Technique In Different Bases

Total Page:16

File Type:pdf, Size:1020Kb

Today We Learn About A Counting Technique In Different Bases

Counting Algorithm

Today we learn about a counting technique in different bases. The typical bases are: - Decimal base symbols = { 0, 1,2,3,4,5,6,7,8,9} - Binary base symbols = { 0, 1} - Octal base symbols = { 0, 1,2,3,4,5,6,7} - Hexadecimal base symbols = { 0, 1,2,3,4,5,6,7,8,9, A, B, C, D, E, F}

Decimal Counting Observation:

We learned how to count in decimal since we were kids. 1. We start at the base and finish a single digit. 2. Now to continue counting we need 2 digits; a. We start to use 1 , not zero, on the 2nd digit. We have 10, 11 12,..19. b. Again the first digit (on the right) we run out. We look at the 2nd digit; it is one. Then we change it to 2; we will get more counts: 20, 21,..29. c. We repeat the process of 2b until we get 90..99. At this moment, we are at the end of two digits when every digits are the last symbol in the base. 3. We now proceed with 3 digits and repeat the process in Step 2. Question: In 2a, after finishing the single digit, why do we start the second digit with one, instead of zero?

The above algorithm is merely the human habit in counting. We must clarify it to bring it near the computer language. Here is my version:

0. We have a base- a set of symbols such as 0, 1,2,3,4,5,6,7,8,9. This set of symbols is an example. The algorithm for counting should work for any set of symbols. A quick observation that we need at least 2 symbols. 1. The first number is the first symbol in the base. I like to interpret another way. Let all the numbers we want to count have a length of 16 digits. Hence the first number is the one with all 16 digits initialized as the first symbol in the base. That means we print out the number 0 000 000 000 000 000 2. Construct the next number: a. Look at the least significant digit (the right most) of the just-printed number. b. If this digit is not the last symbol in the base,  we change this digit to be the next symbol in the base.  In this case we get a next number already; print the number and proceed step 2 again. c. If this digit is the last symbol in the base,  we change this digit to be the first symbol in the base.  In this case we don’t get a next number yet.  We then look at the next significant digit on the left side. This is the step 2 a we are repeating. Hence 2a, 2b, 2 c are repeated until 2b is satisfied. Walk through the algorithm: Let go through the algorithm for the first few number. Step 1: we get the first number 0 000 000 000 000 000 Step 2: a. indicates the digit is 0; b. indicates this digit is not the last symbol in the base, hence it is set to be 1; we print the number out which is 0 000 000 000 000 001 Step 2: a. indicates the digit is 1; b. indicates this digit is not the last symbol in the base, hence it is set to be 2; we print the number out which is 0 000 000 000 000 002 Step 2 will be repeated so we get the following numbers:

spham(Counting Algorithm) Page 1 12/14/2017 0 000 000 000 000 009

Step 2: a. indicates the digit is 9; b. indicates this digit is the last symbol in the base, then step 2a is skipped. c. Hence this is set to be 0; we look at the next digit (2nd from the right side of the number) and repeat the Step 2 Step 2: a. indicates the digit is 0 (the 2nd digit); c. indicates this digit is not the last symbol in the base, hence it is set to be 1; we print the number out which is 0 000 000 000 000 010

You are going to write a program to count numbers in the different bases. Remember to work on a general case such as an arbitrary base with different set of symbols. The verbal algorithm is discussed in class. However, you need to write it in a pseudo-code. Here is a some leading hints for you to start with.

// Level 0: main body // Declare a set of symbols and get it from keyboard: Base[0..MaxBaseSize] // Declare a of certain digits (e.g, 16 digits). Initialize all digits to be the first symbol in the base: Number [0..MaxNumSize] = { Base[0],…, Base[0]}

// Note: for the purpose of divide and conquer, you might want to initialize all the above // declaration and initialize them by the constant. Later when the program is running, you are // going to read from the keyboard and generalize it.

// The output is the array Number and it is changed when the counting is repeated. // The variable counter is used to keep track the printed numbers. It is started 0 until it // reaches a predefined limit which might be given by the user via keyboard.

counter = 0; do { // printout Number < Level 0.1>

// Set Number to be the next counting

// Next counter counter = counter +1; } while ( counter < the predefined limit);

Level 0.1: printout Number // printout the array in one line for (I=0; I

spham(Counting Algorithm) Page 2 12/14/2017 Note: You must modify so that your program will not printout the heading of 0’s. For example 00001278 should be printed 1278 instead. In the general case, the Number is printed without the heading of the first symbol. Base [0].

: Set Number to be the next counting This is a main part of the program. The main idea is to walk from the right to the left of the array Number to inspect the symbol and change it. How do we change it? If the location has a symbol which is NOT the last one in the then we set it to be the next one in the Baseand in this case we get a next number already. If not (else case of the last symbol in Base), we set it to be the first symbol of the Base and continue walking toward left to inspect the next location of the array Number. Here is the pseudo-code:

: Set Number to be the next counting

index  the most right position of array Number ; in Java: Number.length –1. foundNextNumber false; False indicates the next number is not discovered yet True indicates the next number is discovered; It is stored in Mumber. position  0; This will locate the position of symbol Number[index] within the array Base do { // locate position while (number[index] NOT equal Base[position]) { position  position +1 }

// inspect position if (position is NOT the right most place in the array Base) then number[index]  base[position +1] foundNextNumber  true else number[index]  base[0] (first symbol in Base) index  index –1 (walk toward left of Number) } while (getOut is true)

Possible Bug:

If you implement this algorithm, you probably find an error. The numbers are not printed properly when the digit runs out of symbols. If you look at in Level 0.2, the variable position is not re-initialized in the case of position be the right most place in the array Base. Hence a quick way to fix it is to re-initialized in the else-section: // inspect position if (position is NOT the right most place in the array Base) then number[index]  base[position +1] foundNextNumber  true else number[index]  base[0] (first symbol in Base) index  index –1 (walk toward left of Number) position  0

If you pay more attention to the do-while loop, you will recognize that the program exit this loop when the next number is found ( variable foundNextNumber is true). In the else-case, the next number is not found yet, the loop is repeated with the next digit of the number (number [index-1]). Hence, we are able to reinitialize position within the loop at the beginning. Here is a modificatin:

spham(Counting Algorithm) Page 3 12/14/2017 : Set Number to be the next counting

index  the most right position of array Number ; in Java: Number.length –1. We note that Number[index] is the most significant digit. foundNextNumber false; False indicates the next number is not discovered yet True indicates the next number is discovered; the next number is stored in Number. do { // locate position position  0; This will locate the position of Number[index] within the array Base.

while (number[index] NOT equal Base[position]) { position  position +1 }

// inspect position if (position is NOT the right most place in the array Base) then number[index]  base[position +1] foundNextNumber  true else number[index]  base[0] (first symbol in Base) index  index –1 (walk toward left of Number) } while (foundNextNumber is true)

Question: Why do we need to initialize position to be 0, but not other integer? Why the initialization is in, not above, the do..while loop?

spham(Counting Algorithm) Page 4 12/14/2017

Recommended publications