From fb6caf3489d33bcd6701e13175e7ad908d00965e Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 11 Dec 2024 17:10:04 -0700 Subject: Format the first question of assignment 2 --- 3431709-assignment-2.pdf | Bin 0 -> 130249 bytes assignments/2-solution.md | 102 +++++++++++++++++++++++++--------------------- 2 files changed, 56 insertions(+), 46 deletions(-) create mode 100644 3431709-assignment-2.pdf diff --git a/3431709-assignment-2.pdf b/3431709-assignment-2.pdf new file mode 100644 index 0000000..ed1a3e2 Binary files /dev/null and b/3431709-assignment-2.pdf differ diff --git a/assignments/2-solution.md b/assignments/2-solution.md index 872a910..c94f32a 100644 --- a/assignments/2-solution.md +++ b/assignments/2-solution.md @@ -6,15 +6,7 @@ Choose ONE exercise each from Chapters 4 and 5 Chapter 4: -1. Given our discussion of positional numbering systems in Section 4.2.1, see whether you can determine the decimal value of the following numbers: - - a. 133 (base 4) - - 31 (base 10) - b. 367 (base 8, also called octal) - - 247 (base 10) - c. 1BA (base 16, also called hexadecimal. B is the digit that represents 11; A is the digit that represents 10.) - - 442 (base 10) - +```plaintext | Base (N) | N^8 | N^7 | N^6 | N^5 | N^4 | N^3 | N^2 | N^1 | | ---- | - | - | - | - | - | - | - | - | | 2 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 | @@ -22,49 +14,67 @@ Chapter 4: | 8 | 2097152 | 262144 | 32768 | 4096 | 512 | 64 | 8 | 1 | | 10 | 10,000,000 | 1,000,000 | 100,000 | 10,000 | 1,000 | 100 | 10 | 1 | | 16 | 268,435,456 | 16,777,216 | 1,048,576 | 65,536 | 4,096 | 256 | 16 | 1 | - -```irb -irb(main):025> 8.times.map { |x| 2**x }.reverse -=> [128, 64, 32, 16, 8, 4, 2, 1] -irb(main):031> 8.times.map { |x| 4**x }.reverse -=> [16384, 4096, 1024, 256, 64, 16, 4, 1] -irb(main):032> 8.times.map { |x| 8**x }.reverse -=> [2097152, 262144, 32768, 4096, 512, 64, 8, 1] -irb(main):026> 8.times.map { |x| 10**x }.reverse -=> [10000000, 1000000, 100000, 10000, 1000, 100, 10, 1] -irb(main):037> 8.times.map { |x| 16**x }.reverse -=> [268435456, 16777216, 1048576, 65536, 4096, 256, 16, 1] ``` - a. - | 64 | 16 | 4 | 1 | - | -- | -- | - | - | - | 0 | 1 | 3 | 3 | + ```ruby + 8.times.map { |x| 2**x }.reverse + => [128, 64, 32, 16, 8, 4, 2, 1] + 8.times.map { |x| 4**x }.reverse + => [16384, 4096, 1024, 256, 64, 16, 4, 1] + 8.times.map { |x| 8**x }.reverse + => [2097152, 262144, 32768, 4096, 512, 64, 8, 1] + 8.times.map { |x| 10**x }.reverse + => [10000000, 1000000, 100000, 10000, 1000, 100, 10, 1] + 8.times.map { |x| 16**x }.reverse + => [268435456, 16777216, 1048576, 65536, 4096, 256, 16, 1] + ``` + +1. Given our discussion of positional numbering systems in Section 4.2.1, see whether you can determine the decimal value of the following numbers: + + a. 133 (base 4) = 31 (base 10) + + ```plaintext + | 64 | 16 | 4 | 1 | + | -- | -- | - | - | + | 0 | 1 | 3 | 3 | + ``` + + ```ruby + (1*16) + (3*4) + (3*1) + => 31 + 31.to_s(4) + => "133" + ``` + + b. 367 (base 8, also called octal) = 247 (base 10) - ```ruby - (1*16) + (3*4) + (3*1) - => 31 - ``` + ```plaintext + | 512 | 64 | 8 | 1 | + | --- | -- | - | - | + | 0 | 3 | 6 | 7 | + ``` - b. - | 512 | 64 | 8 | 1 | - | --- | -- | - | - | - | 0 | 3 | 6 | 7 | + ```ruby + (3*64) + (6*8) + (7*1) + => 247 + 247.to_s(8) + => "367" + ``` - ```ruby - (3*64) + (6*8) + (7*1) - => 247 - ``` + c. 1BA (base 16, also called hexadecimal. B is the digit that represents 11; A is the digit that represents 10.) = 442 (base 10) - c. - | 4,096 | 256 | 16 | 1 | - | ----- | --- | -- | -- | - | 0 | 1 | B | A | - | 0 | 1 | 11 | 10 | + ```plaintext + | 4,096 | 256 | 16 | 1 | + | ----- | --- | -- | -- | + | 0 | 1 | B | A | + | 0 | 1 | 11 | 10 | + ``` - ```ruby - (1*256) + (11*16) + (10*1) - => 442 - ``` + ```ruby + (1*256) + (11*16) + (10*1) + => 442 + 442.to_s(16) + => "1ba" + ``` Chapter 5: -- cgit v1.2.3