From 95c4b6e1450bc47cc1e086e8f243121eb613a577 Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 22 Sep 2020 21:16:11 -0400 Subject: [PATCH] Left off on 2.4 Declarations. At some point work on the octal/hex converters --- ch_02/hex_and_octal_constants/main_template.c | 49 +++++++++++++++++++ ch_02/hex_converter/main_template.c | 10 ++++ ch_02/octal_converter/main_template.c | 10 ++++ 3 files changed, 69 insertions(+) create mode 100644 ch_02/hex_and_octal_constants/main_template.c create mode 100644 ch_02/hex_converter/main_template.c create mode 100644 ch_02/octal_converter/main_template.c diff --git a/ch_02/hex_and_octal_constants/main_template.c b/ch_02/hex_and_octal_constants/main_template.c new file mode 100644 index 0000000..a196377 --- /dev/null +++ b/ch_02/hex_and_octal_constants/main_template.c @@ -0,0 +1,49 @@ +/* + * + * Quickly demonstrating octal and hex contstants + * + * Note: Octal and Hex values are read right to left. + * + * Author: Kun Deng + */ + + + +#include + + +int main() +{ + int some_value = 25; + // To get the octal value. Follow these steps + // 1. Divide the value 25 by 8 + // 2. The quotient is 3 and the remainder is 1 + // 3. 1 would be the first octal value + // 4. Take the quotient and divide that by 8 + // 5. The quotient of that is 0 and the remainder is 3. Making the second octal value 3 + // 6. The result is 031 + int some_value_octal = 031; + // To get the Hex value. Follow these steps + // 1. Divide the value 25 by 16. + // 2. The quotient is 1 and the remainder is 9 + // 3. The first hex value is 9 + // 4. Divide by the quotient 1 and divide by 16. The remainder is 1 + // 5. The second hex value is 1. + // 6. The result is 0x19 + int some_value_hex = 0x19; + + if (some_value == some_value_octal) + { + printf("%10s%10s\n", "Decimal", "Octal"); + printf("%10d%10d\n", some_value, some_value_octal); + } + + if (some_value == some_value_hex) + { + printf("%10s%10s\n", "Decimal", "Hex"); + printf("%10d%10d\n", some_value, some_value_hex); + } + + + return 0; +} diff --git a/ch_02/hex_converter/main_template.c b/ch_02/hex_converter/main_template.c new file mode 100644 index 0000000..d432873 --- /dev/null +++ b/ch_02/hex_converter/main_template.c @@ -0,0 +1,10 @@ +/* + * + * Write a program that can convert a decimal to a hex value and vice-versa. + * The program must pass a command line argument to determine what will + * be the conversion type and the value to be converted. The value + * must be evaluated to make sure it's valid before converting. + * Otherwise, inform the user and terminate the program + * + * Author: Kun Deng + */ diff --git a/ch_02/octal_converter/main_template.c b/ch_02/octal_converter/main_template.c new file mode 100644 index 0000000..d432873 --- /dev/null +++ b/ch_02/octal_converter/main_template.c @@ -0,0 +1,10 @@ +/* + * + * Write a program that can convert a decimal to a hex value and vice-versa. + * The program must pass a command line argument to determine what will + * be the conversion type and the value to be converted. The value + * must be evaluated to make sure it's valid before converting. + * Otherwise, inform the user and terminate the program + * + * Author: Kun Deng + */