Problem 6: Programming exercise (for all) The goal of this lab exercise is to im
ID: 3742765 • Letter: P
Question
Problem 6: Programming exercise (for all) The goal of this lab exercise is to implement an error-detection mechanism using the standard CRC algorithm described in the text. Write two programs, generator and verifier. The generator program reads from standard input (or a file) a line of ASCII text containing an n-bit message consisting of a string of Os and 1s. The second line is the k-bit polynomial, also in ASCII. It outputs to standard output (or a file) a line of ASCII text withn + k 0s and ls representing the message to be transmitted. Then it also outputs the polynomial, just as it read it in. The verifier program reads in the output of the generator program and outputs a message indicating whether it is correct or not. Finally, write a program, alter, that takes the outputs of the generator program, inverts 1 bit on the first line depending on its argument (the bit number/position, counting from the leftmost bit as 1) but copies the second line correctly. By running generator on the input file and polynomial, and then run verifier on the output of generutor, you should see that the message is correct, but by running generator on the input file and polynomial, and then alter arg on the output of generator, and run verifier again, you should get the error message Note: we will use our own input to test your progran (file size will be no longer than 1024 bits (i.e., 128 characters) and the polynomial will be less than or equal to 8 degrees). So make sure that your program works for general inputs and not only for one specific input. Grading will be based on correctness of the outputs, code, and your program logic. In your homework submission, please write a description of the logic flow of your implementation, and submit both the solutions and the code in D2L. Preferred programming languages are C/C++, or Matlab.Explanation / Answer
#include<iostream>
using namespace std;
string generator(string msg,string poly) //Function to generate the message to be transmitted
{
string trans=msg; //trans contains message to be transmitted
for(int i=0;i<poly.length()-1;i++) //Adding zeros at the end of message
trans+='0';
for(int i=0;i<msg.length();i++)
{
if(trans[i]=='0') //if the bit in trans is 0, then we move forward
continue;
for(int j=0;j<poly.length();j++) //bitwise XORing the message with polynomial
{
if(poly[j]==trans[i+j])
trans[i+j]='0';
else trans[i+j]='1';
}
}
for(int i=0;i<msg.length();i++) //writing the original message to trans and leaving the remainder as it is
trans[i]=msg[i];
return trans;
}
void verifier(string trans,string poly) //Function to check whether message is correct or not
{
for(int i=0;i<=trans.length()-poly.length();i++) //Repeating the same method as of generator with trans
{
if(trans[i]=='0')
continue;
for(int j=0;j<poly.length();j++)
{
if(poly[j]==trans[i+j])
trans[i+j]='0';
else trans[i+j]='1';
}
}
int flag=0;
for(int i=0;i<trans.length();i++) //for message to be correct, remainder should be 0
{
if(trans[i]!='0')
flag=1;
}
if(flag==0)
cout<<"The transmitted message is correct ";
else
cout<<"The transmitted message is not correct ";
}
void alter(string &trans,int n) //Function for flipping message's nth bit
{
if(trans[n-1]=='0')
trans[n-1]='1';
else trans[n-1]='0';
}
int main()
{
string msg,poly,trans; //msg stores n-bit message, poly stores k-bit polynomial and trans stores n+k bit message to be transmitted
cout<<"Enter the message: ";
cin>>msg;
cout<<" Enter the polynomial: ";
cin>>poly;
trans=generator(msg,poly);
cout<<endl<<"The transmitted message is: "<<trans<<endl;
cout<<"The original message was: "<<msg<<endl;
verifier(trans,poly);
alter(trans,2); //flipping the 2nd bit in trans
cout<<" After flipping ";
verifier(trans,poly);
return 0;
}