Created
February 7, 2016 15:29
-
-
Save Valindo/169edd865a0b914a2fe2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string> | |
int main() | |
{ | |
int no=0,i,cnt=0,bcnt=0,alt=1; | |
char hamm[20]; | |
string data; | |
hamm[0]=NULL; | |
printf("Enter data:"); | |
scanf("%c",&data); | |
for(i=1;i<=data.length()+4;i++) | |
{ | |
if(i==1 || i==2 || i==4 ||i==8) | |
hamm[i-1]=' '; | |
else | |
{ | |
hamm[i-1]=data[cnt]; | |
cnt++; | |
} | |
} | |
hamm[data.length()+4]='\0'; | |
int n=0;alt=1; | |
while(alt<=8) | |
{ | |
bcnt=0; | |
n=alt-1; | |
for(i=n;i<data.length()+4;) | |
{ | |
if(alt%2==1) // for 1st position i.e. odd position | |
{ | |
if(hamm[i]=='1') | |
bcnt+=1; | |
else if(hamm[i]=='0') | |
bcnt+=0; | |
i+=(alt*2); | |
} | |
else if(alt%2==0) // for other positions i.e. even positions | |
{ | |
if((i/alt)%2==0 && i%2==1) //((odd/2)%alt==0 || ((even-1)/2)%alt==0) | |
{ | |
if(hamm[i]=='1') | |
bcnt+=1; | |
else if(hamm[i]=='0') | |
bcnt+=0; | |
} | |
else if(((i-1)/alt)%2==0 && i%2==0) | |
{ | |
if(hamm[i]=='0') | |
bcnt+=0; | |
else if(hamm[i]=='1') | |
bcnt+=1; | |
} | |
i++; | |
} | |
} | |
//cout<<bcnt<<endl; | |
if(bcnt%2==0 && alt%2==0) | |
hamm[alt-1]='0'; | |
else if(bcnt%2==1 && alt%2==0) | |
hamm[alt-1]='1'; | |
else if(bcnt%2==0 && alt%2==1) | |
hamm[0]='0'; | |
else if(bcnt%2==1 && alt%2==1) | |
hamm[0]='1'; | |
cout<<endl<<hamm<<endl; | |
alt*=2; | |
} | |
printf("The Hamming code of %s is %c",data,hamm); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment