Question
C++: Create a program using the Tzotil number system (base 20). The program will convert from Tzotzil to Decimal and back again along with doing
C++: Create a program using the Tzotil number system (base 20). The program will convert from Tzotzil to Decimal and back again along with doing a simplified add.
Input
This program does not require user input.
Background
The program will represent 10-19 by A J.
Tzotzil | 9 | A | B | C | D | E | F | G | H | I | J |
Decimal | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 |
Each digit in a Tzotzil number is multiplied by 20 raised to the corresponding power.
The Tzotzil numbers are going to be arrays of 5 characters. Reserve one of those characters for the \0. Simplify the situation by assuming all digits are filled. For example 9 in Tzotzil would be "0009".
The numbers can be at most 4 digits, so the biggest number, "JJJJ", is 159999. Do not worry about negative numbers.
Maintain the array in human-readable format. (IOW in the example "1009", array [0] is 1, array[4] is 9 and array [5] is '\0'.)
Logic
Create an array of 5 character elements to represents a Tzotzil number. You must use a constant to indicate the array size of 5 I called it TZOTZIL_SIZE .
Create the following functions listed below. Your signatures MUST match those given below. There is a spot in the CPP at the bottom to put your functions.
// Convert a decimal integer to a Tzotzil number.
void ConvertFromDecimal(char result[], int i);
// Convert Tzotzil number to a decimal. Note the pointer notation
int ConvertToDecimal(const char *);
void add(char result[], const char a[], const char b[]);
ConvertFromDecimal A function to convert a decimal integer to a Tzotzil number.
To convert a decimal number to Tzotzil, repeatedly divide by 20 and save the remainder in the array working from right to left. To convert 201 (00a1 in Tzotzil ) from decimal to Tzotzil:
Step | Division | Result | Remainder | Tzotzil Number |
1 | 201 / 20 | 10 | 1 | 0001 |
2 | 10 / 20 | 0 | 10 | 00a1 |
Or to convert 745, (01h5 in Tzotzil )
Step | Division | Result | Remainder | Tzotzil Number |
1 | 745 / 20 | 37 | 5 | 0005 |
2 | 37 / 20 | 1 | 17 | 00h5 |
3 | 1 / 20 | 0 | 1 | 01h5 |
Pseudocode:
set Array to all 0.
Set a working variable (work_b) = input variable.
Set a working loop variable j to (TZOTZIL_SIZE 2)
Loop while j > 0 and work_b > 0
Set Array[j] to work_b modulo 20
Divide work_b = work_b / 20
ConvertToDecimal A function to convert the Tzotzil number to a decimal.
Moving from right to left multiply the Tzotzil digit by the appropriate power of 20 and add it to the total. (200 = 1). So for example 0ab0 (4220):
Step | Tzotzil Number | Result | Sum | |
1 | 0ab0 | 0 * 200 | 0 | 0 |
2 | 0ab0 | 11 * 201 | 220 | 220 |
3 | 0ab0 | 10 * 202 | 4000 | 4220 |
4 | 0ab0 | 0 * 203 | 0 | 4220 |
Add This function should take two Tzotzil numbers and add them together. It should return the result in the array result. The result should be all 0 if two integers will overflow the array.
Simplify this calculation. Assuming
add(char result[], const char a[], const char b[])
The pseudocode is:
If overflow
Zero the result.
If not overflow
Convert a to a decimal.
Convert b to a decimal.
Result = (a + b) converted to Tzotzil.
Testing
Testing functions are provided. They can be turned on and off with the bools at the top of the program. A spot is predefined to add any tests additional. The code works if you pass all of the tests. There are marks where you can change things.
There is also const bool PRINT_ALL = true; .
If set to true the program will print all the passing tests. If it is false it will only print the failing tests. In the beginning, you will want to see all the tests.
The order of events is:
- Create stub functions for all three functions. In the case of ConvertToDecimal where you have an int return value return something odd that is not in the test. IE 78787878 or similar
- Define ConvertFromDecimal
- Turn on the Convert From Decimal Tests.
- Pass all of these tests before moving on.
- Define ConvertToDecimal
- Turn on the Convert From Decimal Tests.
- Pass all of these tests before moving on.
- Define Add function
- Turn on the ADD tests
- Pass all of these tests before moving on.
- Add the overflow processing
- Turn on the Overflow tests
Output
The testing routines will print output for you. Do not touch them
Here is a link for the code since it is too long to put into this post: https://pastebin.com/9PckDtEY
Here is a link for the rest of the spreadsheets and visual studio program: https://www.dropbox.com/sh/9qxf52sgj5fucbt/AAAUnr4W6Ks9qaPGyJYnpQVBa?dl=0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started