You are on page 1of 4

/*----------------------------------------------------------------

NAME :GAURAV KHANDELWAL ROLL NO. :08BCE131


collage: NIRMA UNIVERSITY
AIM : TO IMPLEMENT HAMMING CODE SENDER & RECIVER
-------------------------------------------------------------------*/
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
char data[15];
int ans_int[100],count=0;
int m,k_count=0,i_count=0,**result;
void bin_conv(int s)
{
while(s!=0)
{
ans_int[count]=s%2;
s/=2;
count++;
}
}
void code_insert(int r)
{
int temp=0;
if(count%m!=0)
temp=1;
cout<<"\nno. of frames:"<<count/m+temp<<endl;
result=new int *[count/m+temp];
cout<<"no. of parity bits are:"<<r<<endl;
for(int o=0;o<count/m+temp;o++)
{
int l=0;
result[o]=new int[m+r+1];
for(int i=1;i<m+r+1;i++)
{
if(i==pow(2,l))
{
l++;
result[o][i]=0;
}
else
{
result[o][i]=ans_int[k_count];
k_count++;
}
}
int p_count[10]={0};
for(int j=1;j<=r;j++)
{
for(i=pow(2,j-1);i<m+r+1;)
{
for(int n=0;n<pow(2,j-1);n++)
{
if(i+n>m+r)
break;
if(result[o][i+n]==1)
p_count[j-1]++;
}
i=i+pow(2,j);
}
}
for(j=0;j<l;j++)
{
int y=pow(2,j);
result[o][y]=p_count[j]%2;
}
cout<<"-->";
for(j=m+r;j>0;j--)
cout<<result[o][j];
}
}
void generate_error(int r)
{
gotoxy(10,10);
cout<<"error genration process is now started";
int e=rand()%(count/m);
int e_r=rand()%m+r;
cout<<"\n";
cout<<"error has been generated on"<<e<<","<<e_r<<"\n";
result[e][e_r]=(result[e][e_r]+1)%2;
}
void generate(int r,int o)
{
int l=0;
for(int i=1;i<m+r+1;i++)
{
if(i==pow(2,l))
{
l++;
}
else
{
ans_int[i_count]=result[o][i];
i_count++;
}
}
}
void code_interprete(int r)
{
int temp=0;
if(count%m!=0)
temp=1;
for(int o=0;o<count/m+temp;o++)
{
int p_count[10]={0};
for(int j=1;j<=r;j++)
{
for(int i=pow(2,j-1);i<m+r+1;)
{
for(int n=0;n<pow(2,j-1);n++)
{
if(i+n>m+r)
break;
if(result[o][i+n]==1)
p_count[j-1]++;
}
i=i+pow(2,j);
}
}
for(j=0;j<r;j++)
{
if(p_count[j]%2==0)
{
result[o][pow(2,j)]=0;
}
}
int tracker=0;
for(j=0;j<r;j++)
tracker+=(pow(2,j)*(p_count[j]%2));
if(tracker==0)
generate(r,o);
else
{
clrscr();
cout<<"error found"<<o<<","<<tracker<<"\n";
//cout<<result[o][tracker];
gotoxy(12,7);
for(int i=m+r;i>0;i--)
cout<<result[o][i];
gotoxy(12,8);
for(i=0;i<m+r-tracker;i++)
cout<<"-";
cout<<"|";
gotoxy(m+r-tracker+12,9);
cout<<"|";
result[o][tracker]=(result[o][tracker]+1)%2;
gotoxy(12,10);
for(i=m+r;i>0;i--)
cout<<result[o][i];
cout<<"\nerror corrected\n";
//cout<<result[o][tracker];
generate(r,o);
}
}
}
void main()
{
clrscr();
cout<<"enter the string:";
gets(data);
cout<<"enter the value of m:";
cin>>m;
int i=0,ans1;
while(data[i]!='#')
{
ans1=toascii(data[i]);
bin_conv(ans1);
i++;
}
int x=1;
while((m+x+1)>pow(2,x))
{
x++;
}
code_insert(x);
cout<<"\nif u want to create error then press 1:";
int error;
cin>>error;
if(error==1)
{
generate_error(x);
}
code_interprete(x);
int ans;
char *ans_char=new char [count/7];
for(int j=0;j<count/7;j++)
{
ans=0;
for(int k=j*7;k<(j+1)*7;k++)
{
ans+=ans_int[k]*pow(2,k%7);
}
ans_char[j]=ans;
cout<<ans_char[j];
}
delete ans_char;
delete result;
getch();
}
/*
If you want any program in c or c++ then mail me. I will provide you that progr
am.
For program mail me gaurav_khandelwal2616146@yahoo.com,08bce131@nirmauni.ac.in
THANK YOU.
*/

You might also like