I already have more than half portion of the code below. I need 1. a complete (n
ID: 3819866 • Letter: I
Question
I already have more than half portion of the code below. I need
1. a complete (need to add menu, correct last line in current output........ etc) working code including these test when
A. user chooses even parity transmission.
B. user chooses odd parity transmission.
2. a flowchart
Develop a set of functions using C++ that would implement the HAMMING CODE.
////////////////////////
// hamming code
#include<iostream>
#include<ctime>
using namespace std;
// funtion prototypes
void generator(int a[11]);
void display(int a[], int size);
void populate(int a[11], int b[15]);
void onepos(int despos[15], int hamming[15]);
void binconverter(int decpos[15], int binpos[15][4]);
void display2(int a[15][4]);
void parity(int binpos[15][4]);
int main()
{
// declaration
int randdata[11];
int hamming[15];
int decpos[15];
int binpos[15][4] = { 0 }; // 4 columns becuase 15 (1111)
generator(randdata);
display(randdata, 11);
populate(randdata, hamming);
display(hamming, 15);
onepos(decpos, hamming);
display(decpos, 15);
binconverter(decpos, binpos);
display2(binpos);
parity(binpos);
display2(binpos);
return 0;
}
void generator(int a[11])
{
srand(time(NULL));// new seed everytime
for (int i = 0; i < 11; i++)
{
a[i] = rand() % 2; // random numbers 0's and 1's
}
}
//////////////////////////////////////////////////////
// function that display a 1D array
void display(int a[], int size)
{
for (int i = 0; i < size; i++)
cout << a[i] << " ";
cout << endl;
cout << endl;
cout << endl;
}
void populate(int a[11], int b[15])
{
int j = 0;
for (int i = 0; i < 15; i++)
{
if (i == 0 || i == 1 || i == 3 || i == 7)
{
b[i] = 2; // for check bits
}
else
{
b[i] = a[j];
j++;
}
}
}
void onepos(int decpos[15], int hamming[15])
{
int j = 0;
for (int i = 0; i < 15; i++)
decpos[i] = 0;
for (int i = 0; i<15; i++)
if (hamming[i] == 1)
{
decpos[j] = i + 1;
j++;
}
}
void binconverter(int decpos[15], int binpos[15][4])
{
int j = 3;
int temp; // temp holder
for (int i = 0; i< 15; i++)
{
temp = decpos[i]; // to prevent modification to decpos we use temp
while (temp >0) // conversion to binay starts with LSB
{
if (temp % 2 == 1)
{
binpos[i][j] = 1;
j--; // j decrement going from LSB to MSB
}
else
{
binpos[i][j] = 0;
j--;
}
temp = temp / 2; // integer division is an integer
}
j = 3;
}
}
void display2(int a[15][4])
{
// the loop will visit all the rows of the array
for (int rows = 0; rows < 15; rows++)
{
// the child loop will visit every col
for (int col = 0; col < 4; col++)
{
// displays the contenet of the specific cell (rows, col)
cout << a[rows][col] << " ";
}
// inserts a new line after each row
cout << endl;
}
// new line once done
cout << endl;
cout << endl;
}
void parity(int binpos[15][4])
{
int holder = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 15; j++)
{
if (binpos[i][j] == 1)
holder++;
}
if (holder % 2 == 0)
{
// means that col has even number of 1's
binpos[14][i] = 0;
binpos[15][i] = 1;
}
else
{
// means that col has odd number of 1's
binpos[14][i] = 1;
binpos[15][i] = 0;
}
}
}
Explanation / Answer
I have rewrite it the program
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *link;
};
struct node *first=NULL, *last=NULL, *cur=NULL, *next, *move,*new;
//Creation of linked list for the given data
void create()
{
int i=1;
cur=(struct node*)malloc(sizeof(struct node);
l1:for(i=1;i<=7;i++)
{
printf("Enter the%d", i "bit data");
scanf("%d", &cur->data);
//Checking for binary data
if(cur->data!=0||cur->data!=1)
{
printf("Invalid data ");
goto l1;
}
//Insertion in first position
if(i==1)
{
cur->link=NULL;
first=cur;
last=cur;
next=cur;
}
//Insertion in the middle
elseif(i!=7)
{
next->link=cur;
next=cur;
}
//Insertion at the end
else
{
next->link=cur;
next=cur;
last=cur;
}
}
}
//To generate the parity bits
void cal()
{
int count=0,i,x,flag,a,b,c,d;
move=first;
//Parity bit at position 8. Calculation:
for(x=0;x<3;x++)
{
if(move->data==1)
count++;
move=move->link;
}
if(count%2==0)
flag=0;
else
flag=1;
//Assigns the parity bit
new=(struct node*)malloc(sizeof(struct node);
new->data=flag;
new->link=move->link;
move->link=new;
move=new;
move=move->link;
//Parity bit at position 4.Calculation:
count=0;
flag=0;
for(x=0;x<3;x++)
{
if(move->data==1)
count++;
if(x==0) a=move->data;
if(x==1) b=move->data;
if(x==2) c=move->data;
move=move->link;
}
if(count%2==0)
flag=0;
else
flag=1;
//Assigns the parity bit
new=(struct node*)malloc(sizeof(struct node);
new->data=flag;
new->link=move->link;
move->link=new;
move=new;
move=move->link;
d=move->data;
//Parity bit at position 2:Calculation
count=0;
flag=0;
count =(d+b+a);
if(count%2==0)
flag=0;
else
flag=1;
//Assigns the parity bit
new=(struct node*)malloc(sizeof(struct node);
new->data=flag;
new->link=NULL
move->link=new;
move=new;
//Parity bit at position1:Calculation
count=0;
flag=0;
count=(d+c+a);
if(count%2==0)
flag=0;
else
flag=1;
//Assigns the parity bit
new=(struct node*)malloc(sizeof(struct node);
new->data=flag;
new->link=NULL;
move->link=new;
move=new;
count=0;
flag=0;
}
//detection of errors in transmitted data
void transmit()
{
int i,x,a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4;
int count=0, f1=f2=f3=f4=0;
l2:for(i=1;i<=11;i++)
{
printf("Enter the %d",i "value");
scanf("%d",x);
if(x!=0||x!=1)
goto l2;
elseif(i==1) a1=x;
elseif(i==2) a2=x;
elseif(i==3) a3=x;
elseif(i==4) a4=x;
elseif(i==5) b1=x;
elseif(i==6) b2=x;
elseif(i==7) b3=x;
elseif(i==8) b4=x;
elseif(i==9) c1=x;
elseif(i==10) c2=x;
elseif(i==11) c3=x;
count=c3+c2+c1+b4;
if(count%2==0)
f1=0;
else
f1=1;
count=0;
count=a4+b1+b2+b3;
if(count%2==0)
f2=0;
else
f2=1;
count=0;
count=a2+a3+b2+b3;
if(count%2==0)
f3=0;
else
f3=1;
count=0;
count=a1+a3+b1+b3;
f(count%2==0)
f4=0;
else
f4=1;
count=0;
count=[(f4*1)+(f3*2)+(f2*4)+(f1*8)
if(count>11)
printf("Invalid data");
else
printf("Error present in position : %d",count );
}
void main()
{
int a;
//Creation of interacive menu
do
{
printf("Press 1 to determine Hamming Code generated");
printf("Press 2 to determine error in the given code");
printf("Press 3 to exit");
scanf("%d", a);
if(a==1)
{
create();
cal();
}
if(a==2)
transmit();
if(a==3)
exit(0);
}
while(a!=3);
//End of program
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.