Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, please go through the code provided and give back the correct code. I am getting an error, so please try correcting it and show

Hello, please go through the code provided and give back the correct code. I am getting an error, so please try correcting it and show that it ran and the output of the code on the screen. It is in C program.

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.

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.

ALL MY CODE

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)

{

return ((c >> bitNum) & 1);

}

/*************************************************************************************/

/* 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)

{

return (num >> bitNum) & 1;

}

/*************************************************************************************/

/* 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)

{

/* Checks if the bit is 0 and changes it, if not then keeps it the same. */

*num = *num | 1 << bitNum;

}

/*************************************************************************************/

/* 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)

{

//*c = (*c >> bitNum) & 1;

*c = *c | (1 << bitNum);

}

/*************************************************************************************/

/* 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)

{

/* Logic:

* 0x9 = 1001 | 2 bits set to 1

* 0x11 = 1011 | 3 bits set to 1

*/

int total = 0;

int atNow = 0;

int i;

for (i = 0; i < 16; i++) {

atNow = isCharBitSet(num, i);

if (atNow) total++;

}

// if (isShortBitSet(num, i) == 1) total++;

return total;

}

/*************************************************************************************/

/* 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)

{

/* Flip the bit at i */

*num ^= 1 << bitNum;

return;

}

/*****************************************************************************/

/*

* Compare the bits between the already known masks (P1_MASK, P2_MASK ..etc )

* Comparing usind AND operation, if there is a match then increment sum

*

* Input :

* short modular : the number to use when modulating

* short mask : the given masks

* short num : user provided short

* Output :

* the modular of the sum

*/

int compareBits(short modular, short mask, short num) {

int sum = 0;

for (int i = 0; i < sizeof(mask)*8; i++)

if (isShortBitSet(num, i) & isShortBitSet(mask, i))

sum += 1;

return sum % modular;

}

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); int compareBits(short modular, short mask, 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

/* The following are ANSI Escape Sequences.

* You can find more by typing: man terminfo

* The exact ones under can be found on stackoverflow here:

* http://stackoverflow.com/questions/3219393/stdlib-and-colored-output-in-c

*/

#define ANSI_COLOR_RED "\x1b[31;1m"

#define ANSI_COLOR_GREEN "\x1b[32;1m"

#define ANSI_COLOR_YELLOW "\x1b[33;1m"

#define ANSI_COLOR_BLUE "\x1b[34;1m"

#define ANSI_COLOR_MAGENTA "\x1b[35;1m"

#define ANSI_COLOR_CYAN "\x1b[36;1m"

#define ANSI_BACK_BLACK "\x1b[31;1m"

#define ANSI_COLOR_RESET "\x1b[0m"

#define CLEAR "\033[2J"

/************************************************************************/

// 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(ANSI_COLOR_CYAN"Receive Program written in C "ANSI_COLOR_RESET);

printf(" Hamming Code Implementation v1.0.0 by Daniel Omowaye ");

printf(

ANSI_COLOR_RED

"If you have not already, please run "

ANSI_COLOR_GREEN

"./tran "

ANSI_COLOR_RED

"to generate a message. "

ANSI_COLOR_RESET);

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(ANSI_COLOR_RESET);

printf(" Corrected Transmitted Message: \t " ANSI_BACK_BLACK);

for (i = 0; i < numRead; i++) {

char c;

short2Char(encodedMsg[i], &c);

printf("%c", c);

}

printf(ANSI_COLOR_RESET" ");

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;

int k;

*c = 0;

for (i = 0; i < 8; i++) {

// Reaching into bit positions 3,5,6,7,9,10,11,12

if (i == 0) k = 3;

if (i > 0 && i < 4) k = 4 + i;

if (i > 3 && i < 8) k = 5 + i;

// Checking if bit at k is set aka == 1

bitSet = isShortBitSet( encodedNum, k);

// if bit is 0.skip setCharBit and increment i

if (bitSet == 0) continue;

// Set the correct bit in the decoded char

setCharBit(i, 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

// Get the set default parity

parity = 0;

storedParity = isShortBitSet(*num, 1);

if (compareBits(2, P1_MASK, *num)) parity = 1;

// Comapring before and now (checking if an error occured)

if (storedParity != parity) bitNumber += 1;

// Do the same for the rest

parity = 0;

storedParity = isShortBitSet(*num, 2);

if (compareBits(2, P2_MASK, *num)) parity = 1;

if (storedParity != parity) bitNumber += 2;

parity = 0;

storedParity = isShortBitSet(*num, 4);

if (compareBits(2, P4_MASK, *num)) parity = 1;

if (storedParity != parity) bitNumber += 4;

parity = 0;

storedParity = isShortBitSet(*num, 8);

if (compareBits(2, P8_MASK, *num)) parity = 1;

if (storedParity != parity) bitNumber += 8;

// Flip incorrect bit

flipBitShort(bitNumber, num);

return;

}

TRANSMIT.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

/* The following are ANSI Escape Sequences.

* You can find more by typing: man terminfo

* The exact ones under can be found on stackoverflow here:

* http://stackoverflow.com/questions/3219393/stdlib-and-colored-output-in-c

*/

#define ANSI_COLOR_RED "\x1b[31;1m"

#define ANSI_COLOR_GREEN "\x1b[32;1m"

#define ANSI_COLOR_YELLOW "\x1b[33;1m"

#define ANSI_COLOR_BLUE "\x1b[34;1m"

#define ANSI_COLOR_MAGENTA "\x1b[35;1m"

#define ANSI_COLOR_CYAN "\x1b[36;1m"

#define ANSI_BACK_BLACK "\x1b[31;1m"

#define ANSI_COLOR_RESET "\x1b[0m"

#define CLEAR "\033[2J"

/************************************************************************/

// 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(ANSI_COLOR_CYAN "Transmit Program written in C " ANSI_COLOR_RESET);

printf(" Hamming Code Implementation v1.0.0 by Mohamad Yassine ");

printf(

ANSI_COLOR_RED

"After running this program, please copy the output and run "

"the receive program "

ANSI_COLOR_GREEN

"./recv . "

ANSI_COLOR_RESET);

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): \t" ANSI_COLOR_RED);

for (i = 0; i < numRead; i++) {

printf("%d ", encodedMsg[i]);

}

printf(ANSI_COLOR_RESET " ");

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 bits p1 p2 p4 p8

* Summary:

* 1 - Apply the MASK using the AND operand

* 2 - Count the bits that are the result of the AND

* 3 - Find if ODD or Even

* - ODD : If it was ODD (sum % 2 isn't 0) then

* We have to set that bit to 1 to make

* it even.

* - EVEN: Do nothing.

* 4 - Done.

* */

if (compareBits(2, P1_MASK, *num)) setShortBit(1, num);

if (compareBits(2, P2_MASK, *num)) setShortBit(2, num);

if (compareBits(2, P4_MASK, *num)) setShortBit(4, num);

if (compareBits(2, P8_MASK, *num)) setShortBit(8, num);

return(0);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Internals A Deep Dive Into How Distributed Data Systems Work

Authors: Alex Petrov

1st Edition

1492040347, 978-1492040347

More Books

Students also viewed these Databases questions

Question

b. Will there be one assigned leader?

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago