Question
Please take time to answer and produce correct result to the question.please show resulting code and output on screen The requirement code are shown below.
Please take time to answer and produce correct result to the question.please show resulting code and output on screen The requirement code are shown below.
Grading (100 pts): You can earn points as you submit you assignment. Namely, marks will be added as you correctly complete the assignment. 1. (15 pts) 1.1. (5 pts)Proper documentation, meaning variables, 1.2. (5 pts) ease of reviewing program 1.3. (5 pts) compiles with no errors and no warnings. 2. (15 pts) Functions in the bit_manipulation.c were correctly coded. 3. (25 pts) Transmit program is correct 3.1. correctly setting the parity bits 3.2. Providing an example of the input and output of the program 3.3. Providing a transmitReadMe file that describes how to compile and use the program. 4. (45) Receive program is correct 4.1. Correctly completing the function for error correction 4.2. Correctly completing the function for converting from short integer to char 4.3. Providing an example of the input and output of the program 4.4. Providing a receiveReadMe file that describes how to compile and use the program.
QUESTION
Tasks In this assignment you will write two short programs that would simulate the transmission a message over a communication line: one program would simulate the transmission of a message (an array of characters) and one that simulate the receiving of a message. You will use the skeleton in the files transmit.c and receive.c. Helper functions that manipulate bit data should be written in a file bit_manipulation.c Coding Instructions: 1) Commenting in Code as provided in the slides given in class 2) No usage of global variables. All data must be passed or received via function parameters. 3) Write short and simple functions. 4) Suggestion as you code your small helper functions write small test functions to ensure that the code is correct. This will allow you to focus on the logic of your program without worrying about the simple functions. bit_manipulation file (15 pts) The file contains some functions that are required by the main programs. 1. Review the functions in the file and their purpose 2. Complete the code for each of the functions. 3. Add other functions as needed Transmit program (25 pts) 1. (10 pts) Add a function to bit_manipulation.c that takes as input an a short integer num and returns the number of bits that are set to 1. For example if num is 0x9 then the function returns 2. Here you need to declare the function and also add the prototype to the bit_manipulation.h header file. 2. (15 pts) Using the function that you created in 1 above add the code to the setParityBits function. 3. Use the functions in the bit_manipulation.c file Receive program (45 pts) The provided code can already read the encoded message that is provided into an array of integers. The program uses the output from the Transmit program as input to the receive program. Using the provided skeleton do the following: Assignment 2 COMP 2401 Page7 of 7 1. (20 pts) Write a function to collects (unpacks) the bits from the encoded short integer into a character. The function is int short2Char(short encodedChar, char *c) which takes an encodedChar (a short integer) and updates the character with the correct bits. Namely, it maps bits 3,5,6,7,9,10,11,12 of the encodedChar onto bits 0,1,2,3,4,5,6,7 of c. Review the code of the function char2Short() in the Transmit program. 2. (25 pts) Complete the code of correctCode using the function that you coded in step 1 of the Transmit program and the functions in bit_manipulation.c 3. Use the functions in the bit_manipulation.c file Compiling the programs. Compile the transmit program Here we have two files for each program (e.g., transmit.c and bit_manipulation.c). Compiling the program will be as follows: gcc o tran transmit.c bit_manipulation.c Compiling for the debugger will be as follows: gcc -g o tran transmit.c bit_manipulation.c Compile the receive program Here we have two files for each program (e.g., transmit.c and bit_manipulation.c). Compiling the program will be as follows: gcc o recv receive.c bit_manipulation.c Compiling for the debugger will be as follows: gcc -g o recv receive.c bit_manipulation.c Execution: a. Start the transmit program. The program will prompt the user to enter a message. The program will then print the transmitted message as a sequence of short integers. b. Start the receive message. The program will prompt the user to enter the transmitted message. Here you will have to copy the output from transmit program. The program should output the uncorrected transmitted message and the corrected message.
BIT MANIPULATION.C
/*
File bit_manipulation.c
Purpose :
contains helper functions for manipulating bits
Revisions:
file created: Doron Nussbaum
* /
/************************************************************************/
// INCLUDE FILES
#include "bit_manipulation.h"
/************************************************************************/
//MACROS / DEFINES
/************************************************************************/
// FUNCTION PROTOTYPES
/***************************************************/
//example of a testing function main();
#if 0
int mainTesting(int argc, char *argv[])
{
int rc = 0; // return code
short test = 0;
short modified;
int bitNum; // bit number to be used
test = 0;
bitNum = 2;
modified = test;
flipBitShort(bitNum, &modified);
printf("test_int=%d, flipping bit #=%d, answer should be=%d, test_modified=%d ", test, bitNum, 4, modified);
rc = isShortBitSet(modified, 3);
printf("Bit 3 in %d should not be set. Answer is %d ", modified, rc);
rc = isShortBitSet(modified, 2);
printf("Bit 2 in %d should be set. Answer is %d ", modified, rc);
getchar();
return(0);
}
#endif
/*************************************************************************************/
/* purpose: checks if bit i is set
input:
c - the character to be checked
bitNum - the bit number to be checked
return:
1 if the bit is set to 1
0 if the bit is not set (bit is 0)
*/
int isCharBitSet(char c, int bitNum)
{
// add code
}
/*************************************************************************************/
/* purpose: checks if bit i is set
input:
num - the number to be checked
bitNum - the bit number to be checked
return:
1 if the bit is set to 1
0 if the bit is not set (bit is 0)
*/
int isShortBitSet(short num, int bitNum)
{
// add code
}
/*************************************************************************************/
/* purpose: sets bit bitNum to 1
input:
num - the address of the short integer
bitNum - the bit number to be checked
*/
void setShortBit(int bitNum, short *num)
{
// add code
}
/*************************************************************************************/
/* purpose: sets bit bitNum to 1
input:
c - address of character
bitNum - the bit number to be checked
return:
1 if the bit is set to 1
0 if the bit is not set (bit is 0)
*/
void setCharBit(int bitNum, char *c)
{
// add code
//
}
/*************************************************************************************/
/* count the number of bits in a short
input:
num - a short interger
return
the numer of bits that are set to 1 in num
*/
int countBits(short num)
{
// add code
}
/*************************************************************************************/
/* purpose:
flips bit i in num (set bit i to 1 if the bit is 0 or set bit i to 0 if the bit is 1)
input:
bitNum - the bit number to be flipped
num - address of the short integer
*/
void flipBitShort(int bitNum, short *num)
{
// add code
}
BIT MANIPULATION.H
/*
File bit_manipulation.h
Purpose:header file of functions prototypes
*/
/************************************************************************/
// INCLUDE FILES
#include "stdio.h" #include "stdlib.h" #include "assert.h"
/************************************************************************/ // FUNCTION PROTOTYPES
int isCharBitSet(char c, int bitNum); void setCharBit(int bitNum, char *c); int isShortBitSet(short num, int bitNum); void setShortBit(int bitNum, short *num); void flipBitShort(int bitNum, short *num); int countBits(short num);
RECIEVE.C
/*
File receive.c
Purpose: simulate the receiveing end of a message over the network.
The program converts a message encodded as an array of short integers into characters.
The input message uses 1-bit error correction code.
As part of the simulation, the program also corrects any 1-bit errors in the receiving message.
Revision:
a. Initial code - Doron Nussbaum
*/
/************************************************************************/
// INCLUDE FILES
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "assert.h"
#include "bit_manipulation.h"
/************************************************************************/
//MACROS / DEFINES
#define XSTR(A) STR(A)
#define STR(A) #A
#define MAX_INPUT_LENGTH 8192
#define MAX_MSG_LENGTH 2048
#define P1_MASK 0xaa8 // 0b0101010101000
#define P2_MASK 0xcc8 // 0b0110011001000
#define P4_MASK 0x10e0 // 0b1000011100000
#define P8_MASK 0x1e00 // 0b1111000000000
/************************************************************************/
// FUNCTION PROTOTYPES
void short2Char(short encodedChar, char *c);
void correctCode(short *num);
void readEncodedMessage(short *buf, int len, int *numRead);
/**************************************************************************/
int main(int argc, char *argv[])
{
int rc = 0; // return code
char s[MAX_MSG_LENGTH] = { '\0' }; // message
short encodedMsg[MAX_MSG_LENGTH];
int numRead = 0; // number of characters in the message
int i;
// read the message
printf("Please enter the transmitted message: ");
readEncodedMessage(encodedMsg, MAX_MSG_LENGTH, &numRead);
// print the corrected message
printf(" Transmitted Message: ");
for (i = 0; i < numRead; i++) {
char c;
short2Char(encodedMsg[i], &c);
printf("%c", c);
}
printf(" ");
// correct the message
for (i = 0; i < numRead; i++) {
correctCode(&encodedMsg[i]);
}
// print the corrected message
printf(" Corrected Transmitted Message: ");
for (i = 0; i < numRead; i++) {
char c;
short2Char(encodedMsg[i], &c);
printf("%c", c);
}
printf(" ");
return(0);
}
/***********************************************************************************/
/* reads a message from the user
input
len - maximum length of mesage that should be read
output
receivedMsg - contains the message
numRead - the number of characters in the message
*/
void readEncodedMessage(short *receivedMsg, int len, int *numRead)
{
int i;
int rc;
char s[MAX_INPUT_LENGTH+1];
char *token = NULL;
*numRead = 0;
s[MAX_INPUT_LENGTH] = '\0';
scanf("%"XSTR(MAX_INPUT_LENGTH)"[^ ]s", s);
token = strtok(s, " ");
for (i = 0; token != NULL && i < len;token = strtok(NULL, " "), i++) {
rc = sscanf(token, "%hd", &receivedMsg[i]);
}
*numRead = i; // set the number of read integers
// empty the input buffer
for( ; getchar() != ' '; );
}
/*************************************************************************/
/* collect of a short bits 3,5,6,7,9,10,11,12 to bits 0-7 of a char
input
encodedNum - a short with the bits of c
output
c - the char that is embedded in encodedNum
*/
void short2Char(short encodedNum, char *c)
{
short mask = 0x0001;
int i;
int bitSet;
*c = 0;
for (i = 0; i < 8; i++) {
// set the correct bit in char
// for each bit i do
// if the corrsponding bit in the encodedChar is set
// set the bit at the char c
}
}
/*************************************************************************************/
/* purpose:
performs error corrrections, if needed on the short integer
The function first checks the parity bits for error.
If there is an error then it fixes it.
input
num - the address of the short number
*/
void correctCode(short *num)
{
int mask;
int sum;
int bitNumber = 0; // bit number with the error bit
int parity; // a parity bit either 0 or 1
int storedParity; // the parity bit in the encoded char
// check parity bit p1
// get the bits related to P1 using P1_MASK
// count the bits
// determine if the parity bit should have been set
// get the parity for P1_MASK htat is stored in *num
// compare the calculated parity with the stored parity
// if the two parities are different add 2^0 to bitNumber
// simlilary check parity bit p2
// calculate the parity for P2
// compare the calculated parity with the stored parity
// if the two parities are different add 2^1 to bitNumber
// check parity bit p4
// calculate the parity for P4
// compare the calculated parity with the stored parity
// if the two parities are different add 2^2 to bitNumber
// check parity bit p8
// calculate the parity for P8
// compare the calculated parity with the stored parity
// if the two parities are different add 2^3 to bitNumber
// if necessary flip the bit at position bitNumber
}
TANSMIT.C
/*
File transmit.c
Purpose: simulate a transmission of characters over the network.
It converts a message given as an array of characters to an array of short integers that are sent over the network.
The converted message uses 1-bit error correction code.
As part of the simulation, the program also injects errors to the encoded message by flipping one bit.
Revision:
a. Initial code - Doron Nussbaum
*/
/************************************************************************/
// INCLUDE FILES
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "assert.h"
#include "bit_manipulation.h"
/************************************************************************/
//MACROS / DEFINES
#define MAX_MSG_LENGTH 2048
#define P1_MASK 0xaa8 // 0b0101010101000
#define P2_MASK 0xcc8 // 0b0110011001000
#define P4_MASK 0x10e0 // 0b1000011100000
#define P8_MASK 0x1e00 // 0b1111000000000
/************************************************************************/
// FUNCTION PROTOTYPES
int readMessage(char *s, int len, int *numRead);
int char2Short(char c, short *encodedChar);
int setParityBits(short *num);
/************************************************************************/
int main(int argc, char *argv[])
{
int rc = 0; // return code
char s[MAX_MSG_LENGTH] = { '\0' }; // message
short encodedMsg[MAX_MSG_LENGTH];
int numRead = 0; // number of characters in the message
int i;
// read the message
printf("Please enter a message to transmit: ");
readMessage(s, MAX_MSG_LENGTH, &numRead);
// encode the message
for (i = 0; i < numRead; i++) {
char2Short(s[i], &encodedMsg[i]); // embedd the char in a short int
setParityBits(&encodedMsg[i]); // set the parity bits
}
// add errors to mesage
for (i = 0; i < numRead; i++) {
if (rand()%36000 < 25000) {
int bit = rand() % 13;
flipBitShort(bit, &encodedMsg[i]);
}
}
// print the message
printf(" Transmitted message (short integers): ");
for (i = 0; i < numRead; i++) {
printf("%d ", encodedMsg[i]);
}
printf(" ");
return(0);
}
/***********************************************************************************/
/* reads a message from the user
input
len - maximum length of mesage that should be read
output
s - contains the message
numRead - the number of characters in the message
assumption - s is valid
*/
int readMessage(char *s, int len, int *numRead)
{
int i;
int rc = 1;
*numRead = 0;
for (i = 0; i < len, rc != 0; i++) {
rc = scanf("%c", &s[i]);
if (s[i] == ' ') break;
(*numRead)++;
}
return(0);
}
/*************************************************************************/
/* spreads bits 0-7 of the character c into bits 3,5,6,7,9,10,11,12 of the short encodedChar
input
character c
output
encodedChar - a short with the bits of c
*/
int char2Short(char c, short *encodedChar)
{
short mask = 0x0001;
int i;
int bitSet;
*encodedChar = 0;
for (i = 0; i < 8; i++) {
// check if bit i is set
bitSet = isCharBitSet(c, i);
// set the correct bit in the encoded character
if (!bitSet) continue;
switch (i) {
case 0:
setShortBit(3, encodedChar);
break;
case 1:
case 2:
case 3:
setShortBit(4 + i, encodedChar);
break;
case 4:
case 5:
case 6:
case 7:
setShortBit(5 + i, encodedChar);
break;
default:
printf("This should not happen !!!! ");
assert(0);
}
}
return(0);
}
/*************************************************************************************/
/* sets the parity bits in the variable num
input
num - the short numbe where parithy bits must be set
*/
int setParityBits(short *num)
{
int mask;
int sum;
// set parity bit p1
// get the bits covered by the mask
// count the number of bits
// if the numbef of bits is odd then set the parity bit P1
// set parity bit p2
// get the bits covered by the mask for P2
// count the numbefr of bits and set the parity of P2
// set parity bit p4
// get the bits covered by the mask for P4
// count the numbefr of bits and set the parity of P4
// set parity bit p8
// count the numbefr of bits and set the parity of P8
return(0);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started