Question

All that is needed is to fill in that one area that says "Student provides missing code to compare computed hammingCode[] bits to the codeword[] bits to determine the incorrect bit." in the code below. THAT IS ALL THAT IS NEEDED PLUS SCREENSHOT OF COMPILED OUTPUT AFTERWARDS.

Please TAKE SCREENSHOT of the compiled output which should look similar to the sample one provided.

Problem Complete development of Problem6.c. Sample Input File, Problem6.in 1011 10101010e 01110101o 1111000010101110E 1111000010101110O 011110000110101010011101101010110 Sample Program Dialo CACOURSESICS23501Problems Problem6 Problem6.exe memory-words fileName? Problem6.in Memory word = 1011E Code word Code word Code word -0110011 original -1110011 with error in hit #1 = 0110011 with error corrected Memory word 10101010E Code word Code word Code word -111101001010 original -111101000010 with erro r in bit #9 - 111101001010 with error corrected Menory word 011101010 Code word Code word Code word -1001101 10101 with erro r in bit #6 Memory word 1111000010101110E Code word Code word Code word -001011100000101101110 original 001011000000101101 110 with error in bit #? - 001011100000101101110 with error CoFrected Memory word 11110000101011100 Code word Code word Code word or in bit #1 Memory word011110000110101010011101101010110 Code word Code word Code word Press any key to continue . . .

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

// Problem #6

// Problem6.c

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

#include <stdio.h>

#include <stdlib.h>

#include <stdbool.h>

#include <string.h>

#include <math.h>

#include <time.h>

#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);

   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;

   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 )

   {

   // Ignore memoryWord[0], erase ' ' at end of memoryWord[] string (if any)

      memoryWord[0] = 'X';

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

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

   /*

      Determine and erase parity at end of memoryWord[] string (if any)--an 'E' or 'e'

         suffix means (E)VEN, 'O' or 'o' suffix means (O)DD, and no suffix defaults to EVEN.

      Here's BNF (Backus-Naur Form) that captures the preceding description of a memoryWord[]

         <memoryWord> ::= <bit> { <bit> }* [ (( E | e | O | o )) ]

         <bit>        ::= 0 | 1

   */

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

      {

         parity = EVEN;

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

      }

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

      {

         parity = ODD;

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

      }

      else

         parity = EVEN;

   // Compute codeWord[], m, r based on parity, memoryWord[]; display memoryWord[] and codeWord[]

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

      printf(" ");

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

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

   /*

      Cause a random, single-bit error then show the erroroneous codeWord[];

         correct the single-bit error; display corrected codeWord[]

   */

      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)

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

{

   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;

   BITS hammingCode;

// Computer m, r, n

   *m = strlen(memoryWord)-1;

   *r = R(*m);

   n = *m + *r;

/*

   Place memoryWord[] bits into codeWord[] leaving room for the hammingCode[]

      bits which are temporarily marked 'H'

*/

   mi = 1;

   for (ci = 1; ci <= n; ci++)

   {

      if ( IsPowerOf2(ci) )

         codeWord[ci] = 'H';

      else

      {

         codeWord[ci] = memoryWord[mi];

         mi++;

      }

   }

   codeWord[n+1] = '';

// Place hammingCode[] bits into codeWord[] thereby overwriting each 'H'

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

   for (ri = 1; ri <= *r; 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 <= r; ri++)

      sums[ri] = 0;

   for (ri = 1; ri <= r; ri++)

      for (ci = PowerOf2(ri-1); ci <= n; 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 <= r; ri++)

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

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

      else

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

   hammingCode[r+1] = '';

   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;

// Compute Hamming code bits

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

// Compare computed Hamming code bits to codeWord bits to determine incorrect bit

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

      bits to determine the incorrect bit.

// Toggle 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 < x )

      powerOf2 *= 2;

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

}

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

void ToggleBit(BITS word,const int bit)

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

{

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

}

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

// Functions below are "stolen" from the Dr. Hanna random-related pseudo-library

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

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

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <time.h>
#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);
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;
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 )
{
// Ignore memoryWord[0], erase ' ' at end of memoryWord[] string (if any)
memoryWord[0] = 'X';
if ( memoryWord[strlen(memoryWord)-1] == ' ' )
memoryWord[strlen(memoryWord)-1] = NULL;
/*
Determine and erase parity at end of memoryWord[] string (if any)--an 'E' or 'e'
suffix means (E)VEN, 'O' or 'o' suffix means (O)DD, and no suffix defaults to EVEN.
Here's BNF (Backus-Naur Form) that captures the preceding description of a memoryWord[]
<memoryWord> ::= <bit> { <bit> }* [ (( E | e | O | o )) ]
<bit> ::= 0 | 1
*/
if ( toupper(memoryWord[strlen(memoryWord)-1]) == 'E' )
{
parity = EVEN;
memoryWord[strlen(memoryWord)-1] = NULL;
}
else if ( toupper(memoryWord[strlen(memoryWord)-1]) == 'O' )
{
parity = ODD;
memoryWord[strlen(memoryWord)-1] = NULL;
}
else
parity = EVEN;
// Compute codeWord[], m, r based on parity, memoryWord[]; display memoryWord[] and codeWord[]
ComputeCodeWord(memoryWord,parity,&m,&r,codeWord);
printf(" ");
// int c=1;
char sub[100];
// int length=strlen(&memoryWord[1]);
// while (c < length)
//   {
//     sub[c] = memoryWord[c];
//     c++;
//        }
//        sub[c] = '';

strncpy(sub,&memoryWord[1],strlen(&memoryWord[1])-1);
printf(" Memory word = %s%c ",sub,((parity == EVEN) ? 'E' : 'O'));
strncpy(sub,&codeWord[1],strlen(&codeWord[1])-2);
printf(" Code word = %s original ",sub);
/*
Cause a random, single-bit error then show the erroroneous codeWord[];
correct the single-bit error; display corrected codeWord[]
*/
n = m+r;
bit = RandomInt(1,n);
ToggleBit(codeWord,bit);
strncpy(sub,&codeWord[1],strlen(&codeWord[1])-2);
printf(" Code word = %s with error in bit #%d ",sub,bit);
CorrectSingleBitError(parity,m,r,codeWord);
strncpy(sub,&codeWord[1],strlen(&codeWord[1])-2);
printf(" Code word = %s with error corrected ", sub);
}
fclose(MEMORYWORDS);
system("PAUSE");
return( 0 );
}

//------------------------------------------------------
void ComputeCodeWord(const BITS memoryWord,const PARITY parity,int *m,int *r,BITS codeWord)
//------------------------------------------------------
{
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;
BITS hammingCode;
// Computer m, r, n
*m = strlen(memoryWord)-1;
*r = R(*m);
n = *m + *r;
/*
Place memoryWord[] bits into codeWord[] leaving room for the hammingCode[]
bits which are temporarily marked 'H'
*/
mi = 1;
for (ci = 1; ci <= n; ci++)
{
if ( IsPowerOf2(ci) )
codeWord[ci] = 'H';
else
{
codeWord[ci] = memoryWord[mi];
mi++;
}
}
codeWord[n+1] = NULL;
// Place hammingCode[] bits into codeWord[] thereby overwriting each 'H'
ComputeHammingCode(codeWord,parity,*m,*r,hammingCode);
for (ri = 1; ri <= *r; 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 <= r; ri++)
sums[ri] = 0;
for (ri = 1; ri <= r; ri++)
for (ci = PowerOf2(ri-1); ci <= n; 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 <= r; ri++)
if ( sums[ri]%2 == 0 )
hammingCode[ri] = (parity == EVEN) ? '0' : '1';
else
hammingCode[ri] = (parity == EVEN) ? '1' : '0';
hammingCode[r+1] = NULL;
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;
// Compute Hamming code bits
ComputeHammingCode(codeWord,parity,m,r,hammingCode);
// Compare computed Hamming code bits to codeWord bits to determine incorrect bit
//Student provides missing code to compare computed hammingCode[] bits to the codeword[]
//bits to determine the incorrect bit.
// Toggle 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 < x )
powerOf2 *= 2;
return( (powerOf2 == x) ? true : false );
}
//------------------------------------------------------
void ToggleBit(BITS word,const int bit)
//------------------------------------------------------
{
word[bit] = (word[bit] == '1') ? '0' : '1';
}
//-----------------------------------------------------
// Functions below are "stolen" from the Dr. Hanna random-related pseudo-library
//-----------------------------------------------------
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 );
}

memory-words fileNamefile.txt Memory word 1011E Code word 01100110H original Code word 01000110H with error in bit #3 Code word 01000110H with error corrected Memory word 10101010eE Code word111101001010e original Code word 111101000010e with error in bit #9 Code word 111101000010e with error corrected Memory worde11101010010eE Code word010011100101o original Code word 0100111000010 with error in bit #10 Code word 010011100001o with error corrected Memory word111100001010111OEE Code word001011100000101101110E original Code word 001011100001101101110E with error in bit #12 Code word - 001011100001101101110E with error corrected Memory word111100001010111001110EE Code word0010111000001011011100 original Code word 0010111100001011011100 with error in bit #8 Code word -0010111100001011011100 with error corrected Memory worde1111000011010101001110110101010 Code word Code word 1000111110001111010101001110110110101 with error in bit #13 = 1000111110001111010101001110110110101 with error corrected Press any key to continue

Add a comment
Know the answer?
Add Answer to:
All that is needed is to fill in that one area that says "Student provides missing...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of...

    Implement a rabin_hash method to make the following code work: int rabin_karp_batchmatch(int bsz, /* size of bitmap (in bits) to be used */ int k, /* chunk length to be matched */ const char *qs, /* query docoument (X)*/ int m, /* query document length */ const char *ts, /* to-be-matched document (Y) */ int n /* to-be-matched document length*/) { /*if the to-be-matched document is less than k length, return false*/ if (n < k) return 0; /*start our...

  • IN C ONLY As mentioned earlier there are two changes we are going to make from...

    IN C ONLY As mentioned earlier there are two changes we are going to make from lab 5, The file you read into data structures can be any length. studentInfo array will be stored in another struct called studentList that will contain the Student pointer and current length of the list. Sometimes data can be used in structs that correlate between variables so it's convenient to store the data in the same struct. Instead of tracking a length variable all...

  • I am having problems with the following assignment. It is done in the c language. The...

    I am having problems with the following assignment. It is done in the c language. The code is not reading the a.txt file. The instructions are in the picture below and so is my code. It should read the a.txt file and print. The red car hit the blue car and name how many times those words appeared. Can i please get some help. Thank you. MY CODE: #include <stdio.h> #include <stdlib.h> #include <string.h> struct node { char *str; int...

  • I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I...

    I NEED HELP WITH DEBUGGING A C PROGRAM! PLEASE HEAR ME OUT AND READ THIS. I just have to explain a lot so you understand how the program should work. In C programming, write a simple program to take a text file as input and encrypt/decrypt it by reading the text bit by bit, and swap the bits if it is specified by the first line of the text file to do so (will explain below, and please let me...

  • Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name: True/False: Circle one Assignment...

    Santa Monica College CS 20A: Data Structures with C++ Spring 2019 Name: True/False: Circle one Assignment 1 ID: 1. True / False 2. True / False 3. True / False 4. True / False 5. True / False 6. True / False 7. True / False 8. True / False 9. True / False 10. True / False Variable and functions identifiers can only begin with alphabet and digit. Compile time array sizes can be non-constant variables. Compile time array...

  • Name_year_test.c #include "Name_year.h" #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void print_name_year(const Name_year * ny) {   ...

    Name_year_test.c #include "Name_year.h" #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> void print_name_year(const Name_year * ny) {    printf("%s,%d\n", ny->name, ny->year); } int main() {    char * ny_format = "%s,%d\n"; #if PHASE >= 1    puts("Phase 1 tests");    // Make sure we can create an object and get the data back.    // We're creating the name as a local array so that we can    // make sure that create_name_year() does not merely make    // a copy of...

  • Overview: You will be writing classes that implement a playlist simulation for a music streaming app.The...

    Overview: You will be writing classes that implement a playlist simulation for a music streaming app.The Playlist class will contain a dynamic array of Song objects, and the Song class contains several pieces of information about a song. You will need to: 1) Finish the writing of two classes: Song and Playlist. The full header file for the Song class has been provided in a file called Song.h. 2) Write a main program (filename menu.cpp) that creates a single Playlist...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT