This is a set of practice questions. Feel free to work on these questions, and to ask questions if you face any difficulties in coming up with answers. While collaboration with others on the graded homework is strictly prohibited, you are free to work on these practice questions together with other people.
Since they are practice questions, you do not have to, and should not, submit answers to these questions on Blackboard. These questions will not be considered in any way towards your course grade. At the same time, based on the instructor's experience, individuals spending substantial and systematic effort in answering these practice questions by themselves tend to significantly improve their overall class performance.
Practice Question 1
Complete the program provided at general_codes.c to do encoding and decoding of binary strings using a code stored in a text file. Decoding will do error correction and error detection.
To make it easy to read and modify input and output files using a text editor, our input and output will be TEXT files, not binary files. As an example, consider 3-bit original word 100, and the code defined in file code1.txt, which defines a mapping of 3-bit original words to 6-bit codewords.
- In the text file where we store uncoded data, we store this 3-bit word as string '100'.
- The 6-bit codeword for 100 is 100110.
- In the text file where we store coded data, we store this binary number as string '100110'.
The program takes four command line arguments:
number
: Number is 0 if we want to do encoding, and 1 if we want to do decoding.
code_file
: A file specifying a code, such as code1.txt. The format of this file is as follows:
- First, we have two numbers m and n, separated by space. Number m specifies the number of bits in original words, and number n specifies the number of bits in codewords.
- Then, we have 2m lines, each containing two words separated by space: an m-character original word and an n-character codeword.
input_file
: An input file, from which data is read.
output_file
. An output file, to which data is written.
The program behaves as follows:
- First, the code file is read, and the code (i.e., original words and corresponding codewords) is stored in memory.
- If
number
is 0, then the input file contains characters that are '0' or '1'. Each chunk of m such characters represents an original word. The output file should contain the codewords corresponding to these chunks.
- If
number
is 1, then the input file contains characters that are '0' or '1'. Each chunk of n such characters represents a codeword corresponding to an m-bit original word. The output file should contain the original m-bit word for each codeword. Furthermore, the program should print the positions of the words where errors were detected and corrected, and the positions of the words where errors were detected and not corrected.
You should assume that the input file ONLY contains only characters '0' and '1', nothing else (no spaces, new lines, etc.). You should enforce that your output file follows the same convention, and also contains ONLY characters '0' and '1'.
To help you get started, we provide the following resources:
-
File general_codes.c contains most of the code that you need for the solution. You need to complete that code. My solution contains about 80 lines more than the code that you have been provided, but several of these lines are empty or contain a single brace. In particular, you need to implement the following functions used in the code:
- int hammingDistance(char * code1, char * code2, int length);
- int nearestWord(char ** codes, char * word, int number, int length, int * tie_flag);
- int codeDistance(struct code_struct code);
- void convert_to_codeword(struct code_struct code, char * input_buffer, char * output_buffer);
- int convert_to_original_word(struct code_struct code, char * input_buffer, char * output_buffer);
-
File code1.txt defines a distance-3 code that maps 3-bit original words to 6-bit codewords. We have also seen this code in class.
-
File practice_in1.txt is a text file that contains 16 3-bit original words.
- File out1.txt is the coded version of file practice_in1.txt, according to the code defined in code1.txt.
- File error1.txt is a corrupted version of out1.txt, that includes 6 errors, at codewords 0, 1, 4, 6, 8, 11. Your solution should be able to detect and correct these errors.
- File error2.txt is an even more corrupted version of out1.txt, that includes 7 errors, at codewords 0, 1, 4, 6, 8, 11, 15. Your solution should be able to detect and correct these errors, except for the error at position 15, which should be detected but NOT corrected.
While we have provided these test files, we will test your solutions with different test files, so your solution should be able to handle any valid code and input files.
Practice Question 2
When we transfer data from a big-endian machine to a little-endian machine, do we need to treat 16-bit and 32-bit integers the same way, or not? If not, what should be the difference? Describe how each of the two cases must be handled. Provide specific examples.
Practice Question 3
Suppose that we have an error-detecting code, mapping m-bit original words to n-bit codewords. Suppose that one n-bit codeword has been corrupted to a random n-bit pattern (so, we do not specify here the number of bits that were changed). What is the probability that this error will be detected, as a function of m and n?
Practice Question 4
(This is Problem 15 from Chapter 2 of the textbook).
An extended ASCII character is represented by an 8-bit quantity. The associated Hamming encoding of each character can then be represented by a string of three hex digits. Encode the following extended five-character ASCII text using an even-parity Hamming code: Earth. Show your answer as a string of hex digits.
Practice Question 5
(This is Problem 16 from Chapter 2 of the textbook).
The following string of hex digits encodes extended ASCII characters in an even-parity Hamming code: 0D3 DD3 0F2 5C1 1C5 CE3. Decode this string and write down the characters that are encoded.
Back to the list of assignments.