[Solved] CS240 Homework 3-Integer to Quaternary String

$25

File Name: CS240_Homework_3-Integer_to_Quaternary_String.zip
File Size: 423.9 KB

SKU: [Solved] CS240 Homework 3-Integer to Quaternary String Category: Tag:
5/5 - (1 vote)

The decimal number system probably arises from our tendency of counting with our fingers. It is not the only way. A tribe of people have so many four-legged animals that they use the quaternary number system base 4. The digits are 0, 1, 2, and 3. For example, one, two, three, four, five, six, seven, and eight are represented as 14, 24, 34, 104, 114, 124, 134, and 204. Another tribe love their toes as much as fingers so they use the vigesimal number system base 20. The digits are 0, 1, , 9, A, B, , and J. For example, the decimal numbers 30, 31, and 32 are represented as 1A20, B20, and 1C20.

This assignment is to write C code that converts a positive integer among its quaternary, decimal, and vigesimal representations.

2 Integer to Quaternary String

Write the C function itoq() to covert an integer to a quaternary string. Its prototype is described in the header file itox.h. Write your code in the file itox.c. You can build from the template itox.c on BlackBoard.

The idea behind itoq() is to convert an int variable (an int has 32 bits on our machine) directly to an ASCII string of quaternary digits, 0, , 3. In this assignment, we will consider only positive integers. The algorithm is as follows.

  1. Divide the int by 4.
  2. The remainder is the first quaternary digit, to be placed in quaternaryStr[15]. Use 0 to represent remainder = 0, 1 for remainder = 1, and so on.
  3. Update the int to be the quotient.
  4. Repeat steps 1, 2, and 3 for sizeof(int) * 4 times.

When Step 2 is executed for the second time, the quaternary digit will go into quaternaryStr[14]; when Step 2 is executed for the third time, the hex digit will go into quaternaryStr[13]; and so on.

  1. Terminate quaternaryStr with .

Note that the array quaternaryStr declared by the caller can be declared with a size sizeof(int) * 4 + 1. sizeof() is evaluated at compile time to be the number of bytes in a variable of a given type. For example, it is 4 bytes to an int on our machine, but it might be 8 bytes on a different machine. Thus sizeof() allows your code to be portable.

3 Quaternary String to Integer

Write the C function qtoi() to convert a quaternary string to an integer. Its prototype is also described in itox.h. Write your code in file itox.c. Your code should only accept the valid quaternary digits: 0, , 3. An algorithm is as follows.

  1. Start at the last character in the string, which is the least significant digit.Remember to skip over the character.
  2. Convert the character to its decimal equivalent.For example, character 2 is 2.
  3. Multiply the decimal number by powers of 4.

The last character will be multiplied by 40, the next by 41, and so on.

  1. Repeat for all of the characters in the quaternary string.
  2. Sum the products.

4 Integer to Vigesimal String

Write similar functions for conversions to and from vigesimal strings. The differences are the following:

  • The vigesimal number system is base 20.
  • The digits are 0, , 9, A, , J.
  • From a 32-bit int, there can be at most 8 vigesimal digits. So vigesimalStr[] must have sizeof(int) * 2 + 1 bytes.

5 The Driver

The driver is the main program that will call itoq(), qtoi(), itov(), and vtoi(). This is not the same thing as a device driver. The driver should be named itoxDriver.c, and you can use the stub itoxDriver.c from BlackBoard.

Now you have three files of source code: the header file itox.h, the utility file itox.c, and the driver itoxDriver.c. The following is how you compile multiple files into one executable, and test your code.

gcc -Wall itox.c itoxDriver.c -o itox

./itox < test.txt

You are not allowed to use any integer to string manipulation library functions, such as itoa(), etc.

Write a readMe.txt file, and discuss what you found difficult about this assignment, how you planned your approach to it, and what you learned completing it.

Files to email me:

  1. c
  2. c
  3. txt

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[Solved] CS240 Homework 3-Integer to Quaternary String
$25