Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

C Programming Only Please Help fill in the missing code in the designated area in source files that is labeled with the Student provides missing

C Programming Only

Please Help fill in the missing code in the designated area in source files that is labeled with the Student provides missing code.

#include

#include

#include

#include

#include

#include

#define MAXIMUMM 512

#define MAXIMUMR 10

typedef char BITS[MAXIMUMM+MAXIMUMR+1];

typedef enum { EVEN,ODD } PARITY;

//------------------------------------------------------

int main()

//------------------------------------------------------

{

void ComputeCodeWord(const BITS memoryWord,const PARITY parity,

int *m,int *r,BITS codeWord,BITS hammingCode);

void SetRandomSeed(void);

int RandomInt(const int LB,const int UB);

void ToggleBit(BITS word,const int bit);

void CorrectSingleBitError(const PARITY parity,const int m,const int r,BITS codeWord);

char fileName[80+1];

FILE *MEMORYWORDS;

BITS memoryWord,codeWord,hammingCode;

PARITY parity;

int m,r,n,bit;

printf("memory-words fileName? "); scanf("%s",fileName);

if ( (MEMORYWORDS = fopen(fileName,"r")) == NULL)

{

printf("Error opening memory-words file \"%s\"! ",fileName);

system("PAUSE");

exit( 1 );

}

SetRandomSeed();

while ( fgets(&memoryWord[1],MAXIMUMM,MEMORYWORDS) != NULL )

{

memoryWord[0] = 'X';

if ( memoryWord[strlen(memoryWord)-1] == ' ' )

memoryWord[strlen(memoryWord)-1] = '\0';

if ( toupper(memoryWord[strlen(memoryWord)-1]) == 'E' )

{

parity = EVEN;

memoryWord[strlen(memoryWord)-1] = '\0';

}

else if ( toupper(memoryWord[strlen(memoryWord)-1]) == 'O' )

{

parity = ODD;

memoryWord[strlen(memoryWord)-1] = '\0';

}

else

parity = EVEN;

ComputeCodeWord(memoryWord,parity,&m,&r,codeWord,hammingCode);

printf(" ");

printf("Memory word = %s%c ",&memoryWord[1],((parity == EVEN) ? 'E' : 'O'));

printf("Hamming code = %s ",&hammingCode[1]);

printf("Code word = %s original ",&codeWord[1]);

n = m+r;

bit = RandomInt(1,n);

ToggleBit(codeWord,bit);

printf("Code word = %s with error in bit #%d ",&codeWord[1],bit);

CorrectSingleBitError(parity,m,r,codeWord);

printf("Code word = %s with error corrected ", &codeWord[1]);

}

fclose(MEMORYWORDS);

system("PAUSE");

return( 0 );

}

//------------------------------------------------------

void ComputeCodeWord(const BITS memoryWord,const PARITY parity,

int *m,int *r,BITS codeWord,BITS hammingCode)

//------------------------------------------------------

{

int R(const int m);

bool IsPowerOf2(const int x);

int PowerOf2(const int exponent);

void ComputeHammingCode(const BITS codeWord,const PARITY parity,

const int m,const int r,BITS hammingCode);

int n,ri,mi,ci;

// Computer m, r, n

*m = strlen(memoryWord)-1;

*r = R(*m);

n = *m + *r;

mi = 1;

for (ci = 1; ci

{

if ( IsPowerOf2(ci) )

codeWord[ci] = 'H';

else

{

codeWord[ci] = memoryWord[mi];

mi++;

}

}

codeWord[n+1] = '\0';

ComputeHammingCode(codeWord,parity,*m,*r,hammingCode);

for (ri = 1; ri

codeWord[PowerOf2(ri-1)] = hammingCode[ri];

}

//------------------------------------------------------

void ComputeHammingCode(const BITS codeWord,const PARITY parity,

const int m,const int r,BITS hammingCode)

//------------------------------------------------------

{

bool IsPowerOf2(const int x);

int PowerOf2(const int exponent);

int n = m+r;

int *sums = (int *) malloc(sizeof(int)*(r+1));

int ri,ci;

// Compute sum-of-bits for each hammingCode[] bit

for (ri = 1; ri

sums[ri] = 0;

for (ri = 1; ri

for (ci = PowerOf2(ri-1); ci

if ( !IsPowerOf2(ci) )

if ( ((ci >> (ri-1)) & 0X1) == 1 ) sums[ri] += (codeWord[ci] == '1') ? 1 : 0;

// Compute hammingCode[] bits for EVEN or ODD parity

for (ri = 1; ri

if ( sums[ri]%2 == 0 )

hammingCode[ri] = (parity == EVEN) ? '0' : '1';

else

hammingCode[ri] = (parity == EVEN) ? '1' : '0';

hammingCode[r+1] = '\0';

free(sums);

}

//------------------------------------------------------

void CorrectSingleBitError(const PARITY parity,const int m,const int r,BITS codeWord)

//------------------------------------------------------

{

void ComputeHammingCode(const BITS codeWord,const PARITY parity,

const int m,const int r,BITS hammingCode);

int PowerOf2(const int exponent);

void ToggleBit(BITS word,const int bit);

int ri,bit;

BITS hammingCode;

ComputeHammingCode(codeWord,parity,m,r,hammingCode);

/*

Student provides missing code to compare computed hammingCode[] bits to the codeword[]

bits to determine the incorrect bit.

*/

ToggleBit(codeWord,bit);

}

//------------------------------------------------------

int R(const int m)

//------------------------------------------------------

{

int PowerOf2(const int exponent);

int r = 1;

while ( (m+r+1) > PowerOf2(r) ) r++;

return( r );

}

//------------------------------------------------------

int PowerOf2(const int exponent)

//------------------------------------------------------

{

return( (int) pow(2,exponent) );

}

//------------------------------------------------------

bool IsPowerOf2(const int x)

//------------------------------------------------------

{

int powerOf2 = 1;

while ( powerOf2

powerOf2 *= 2;

return( (powerOf2 == x) ? true : false );

}

//------------------------------------------------------

void ToggleBit(BITS word,const int bit)

//------------------------------------------------------

{

word[bit] = (word[bit] == '1') ? '0' : '1';

}

//-----------------------------------------------------

void SetRandomSeed(void)

//-----------------------------------------------------

{

srand( (unsigned int) time(NULL) );

}

//------------------------------------------------------

int RandomInt(const int LB,const int UB)

//------------------------------------------------------

{

double RandomDouble();

return( (int) ((UB-LB+1)*RandomDouble()) + LB );

}

//--------------------------------------------------

double RandomDouble()

//--------------------------------------------------

{

int R;

do

R = rand();

while ( R == RAND_MAX );

return( (double) R/RAND_MAX );

}

image text in transcribed

Problem Read the section entitled 2.2.4 Error-Correcting Codes" in the text book then complete development of Problemo.c"armed with the theory of single-bit error-correcting Hamming codes. Sample Input File, Problem6.in 1011 10101010e 011101010 1111000010101110E 11110000101011100 011110000110101010011101101010110 Why does the memory word 1011 default to (E)ven parity? Sample Program Dialog F:\COURSES\CS2350\Problems\Problem6\Problem6.exe memory-words fileName? Problem6.in Memory word = 1011E Hamming code = 010 Code word = 0110011 original Code word = 0010011 with error in bit #2 Code word = 0110011 with error corrected Memory word = 10101010E Hamming code = 1110 Code word = 111101001010 original Code word = 111101001011 with error in bit #12 Code word = 111101001010 with error corrected Memory word = 011101010 Hamming code = 1011 Code word = 100111110101 original Code word = 100111111101 with error in bit #9 Code word = 100111110101 with error corrected Memory word = 1111000010101110E Hamming code = 0001 Code word = 001011100000101101110 original Code word = 001011100100101101110 with error in bit #10 Code word = 001011100000101101110 with error corrected Memory word = 11110000101011100 Hamming code = 11110 Code word = 111111110000101001110 original Code word = 111101110000101001110 with error in bit #5 Code word = 111111110000101001110 with error corrected Memory word = 011110000110101010011101101010110 Hanning code = 100111 Code word = 10001111100001110101010011101101101011 original Code word = 10001111100001110101010011101101101111 with error in bit #36 Code word = 10001111100001110101010011101101101011 with error corrected Press any key to continue ... Problem Read the section entitled 2.2.4 Error-Correcting Codes" in the text book then complete development of Problemo.c"armed with the theory of single-bit error-correcting Hamming codes. Sample Input File, Problem6.in 1011 10101010e 011101010 1111000010101110E 11110000101011100 011110000110101010011101101010110 Why does the memory word 1011 default to (E)ven parity? Sample Program Dialog F:\COURSES\CS2350\Problems\Problem6\Problem6.exe memory-words fileName? Problem6.in Memory word = 1011E Hamming code = 010 Code word = 0110011 original Code word = 0010011 with error in bit #2 Code word = 0110011 with error corrected Memory word = 10101010E Hamming code = 1110 Code word = 111101001010 original Code word = 111101001011 with error in bit #12 Code word = 111101001010 with error corrected Memory word = 011101010 Hamming code = 1011 Code word = 100111110101 original Code word = 100111111101 with error in bit #9 Code word = 100111110101 with error corrected Memory word = 1111000010101110E Hamming code = 0001 Code word = 001011100000101101110 original Code word = 001011100100101101110 with error in bit #10 Code word = 001011100000101101110 with error corrected Memory word = 11110000101011100 Hamming code = 11110 Code word = 111111110000101001110 original Code word = 111101110000101001110 with error in bit #5 Code word = 111111110000101001110 with error corrected Memory word = 011110000110101010011101101010110 Hanning code = 100111 Code word = 10001111100001110101010011101101101011 original Code word = 10001111100001110101010011101101101111 with error in bit #36 Code word = 10001111100001110101010011101101101011 with error corrected Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions