Question: Write a C program that carries out the function described in the instructions. Use the code template below to write the code. Do not change

Write a C program that carries out the function described in the instructions. Use the code template below to write the code. Do not change the template: /* lab2.c
* Your Name -- you must change to your name and id
*
*
* PP2
*
* Purpose: A template for PP2-- replace
*
* Assumptions:
* #1: The menu driven input was provided and must be used exactly
* as written. A user can enter commands:
* enc CUt
* dec 0E8A549C
* quit
* Encoding takes three printable ASCII letters
* Decoding takes up to eight HEX digits. If exactly eight digits are
* entered, the first digit must be 0 or 1.
* Leading zeros can be dropped.
*
* #2: The string and character type libraries cannot be used except as
* already provided. These libraries are for checking inputs in main
* and in printing after decoding is complete. They cannot be used
* for anyother purpose.
*
* #3: No arrays can be used (excpet as already provided for collecting
* keyboard input). You must use bitwise operators for all encoding
* and decoding. If you want to use an array as a lookup table you
* must first propose your design and get it approved. Designs that
* use tables to avoid bitwise operators will not be approved. There
* are many good and simple designs that do not require tables.
*
* #4 No changes to the code in main. Your code must be placed in
* functions. Additional functions are encouraged.
*
* Bugs:
*
* See the ECE 2220 programming guide
*
* If your formatting is not consistent you must fix it. You can easily
* reformat (and automatically indent) your code using the astyle
* command. If it is not installed use the Ubuntu Software Center to
* install astyle. Then in a terminal on the command line do
*
* astyle --style=kr lab1.c
*
* See "man astyle" for different styles. Replace "kr" with one of
* ansi, java, gnu, linux, or google to see different options. Or, set up
* your own style.
*
* To create a nicely formated PDF file for printing install the enscript
* command. To create a PDF for "file.c" in landscape with 2 columns do:
* enscript file.c -G2rE -o -| ps2pdf - file.pdf
*/
#include
#include
#include
#include
#define MAXLINE 100
// function prototypes
void encode(unsigned char first_letter, unsigned char second_letter,
unsigned char third_letter);
void decode(unsigned int codeword);
int main()
{
char line[MAXLINE];
char command[MAXLINE];
char inputcs[MAXLINE];
int items;
int i, invalid;
unsigned int codeword;
printf("
PP2: encoding and decoding (29,24) Hamming code.
");
printf("Commands:
\tenc 3-letters
\tdec 8-hex-digits
\tquit
");
// each call to fgets, collects one line of input and stores in line
while (fgets(line, MAXLINE, stdin)!= NULL){
items = sscanf(line,"%s%s", command, inputcs);
if (items ==1 && strcmp(command, "quit")==0){
break;
} else if (items ==2 && strcmp(command, "enc")==0){
// encoding
if (strlen(inputcs)!=3||!isprint(inputcs[0])||
!isprint(inputcs[1])||!isprint(inputcs[2])){
printf("Invalid input to encoder: %s
", inputcs);
} else {
encode(inputcs[0], inputcs[1], inputcs[2]);
}
} else if (items ==2 && strcmp(command, "dec")==0){
// decoding: convert hex digits to integer
items = sscanf(inputcs,"%x", &codeword);
if (items !=1|| strlen(inputcs)>8){
printf("Invalid input to decoder: %s
", inputcs);
} else {
// verify all digits are hex characters because
// scanf does not reject invalid letters
for (i=0, invalid=0; i strlen(inputcs) && !invalid; i++){
if (!isxdigit(inputcs[i]))
invalid =1;
}
// if 8 digits, leading digit must be 1 or 0
if (invalid){
printf("Invalid decoder digits: %s
", inputcs);
} else if (strlen(inputcs)==8 && inputcs[0]!='1'
&& inputcs[0]!='0'){
printf("Invalid decoder leading digit: %s
", inputcs);
} else {
decode(codeword);
}
}
} else {
printf("# :%s", line);
}
}
printf("Goodbye
");
return 0;
}
/* encode: calculates parity bits and prints codeword
*
* input: three ASCII characters
* assumptions: input is valid
*
* Example: if input letters are is 'C','U', and 't'
* the final print must be:
*---01110100010100101010010011100
* Codeword: 0x0E8A549C
*/
void encode(unsigned char first_
Write a C program that carries out the function

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!