How to implement hamming code in C++ or C
I have a prog same as other user (Melissa).
I need help to get me started in coding as i'm almost forgot everything i've learned on C or C++.
The task is implement a program for any hamming codes by using the user input the following:
1. Desired hamming code (n,k)
2. Choose to encode or decode.

There will be a main function, encode function, decode function and menu function.
It will be neat to track errors using function rather than just writing everything in the main function.

if someone can re-explain the basic concept of hamming codes.
i have the basic idea on this, but i almsot forgot on how to code in C or C++..

any kind of help would be highly appreciated

Recommended Answers

All 7 Replies

>> almsot forgot on how to code in C or C++

you need to refresh your memory before continuing or you will get nowhere very fast. Dig out those old text books you bought (you DID keep them didn't you?) and do a few of the end-of-chapter examples.

I have no clue about hamming codes -- never used them, so I'm not any help at all.

ALGORITHM OF THE HAMMING CODE

STEP 1: There is a function created that accepts seven bit data from the user.
It checks whether the data is binary and stores the data in the form of a linked list for easier insertion. Separate if conditions are created for elements in first, middle and last positions.

STEP 2: The next function created, sets even parity by checking the values in various positions. The number of parity bits is given by the formula

2^p>=d+p+1

Where, d is number of data bits (=7)
p is the number of parity bits

The positions to be inserted in is given by the continuous powers of 2, starting from 0(Eg:1,2,4,8,16 and so on). Even parity bits are introduced by comparing suitable values. For instance, the 9th, 10th, and 11th bit are checked for parity, using the variable count, and a variable flag is used to store the new value. Similarly, data is introduced in the 4th position (comparing 5th, 6th, and 7th bit), 2nd position (checking the 3rd, 6th, 7th position) and 1st position (checking 3rd, 5th and 7th position).

STEP 3: The next function, accepts the received data, and checks for any error in the received data by using even parity method as before.

STEP 4: The main function, provides a menu to the user asking whether they want to create a new Hamming Code or check for errors by using the Hamming Code or to exit. Depending on the values entered, suitable functions are called and the codes displayed.

#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 \n");
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
}
commented: only three years too late! -7

Next time, check the date that the thread was created before you post a couple hundred lines of badly formatted code which doesn't even work, especially without code-tags to somebody who probably had their problem solved a couple of years ago. Read this.

hey there

hamming code:

1st if yo ve a n bit data then the no. of parity bit is given by, 2^p>=p+n+1 where p is d no parity bit.

let us assume v ve a 4 bit data 1001. so v ll need 3 parity bits and a total of 7 bits 2 b transmitted

d bits r place in position 2^x where x=0,1,2...p
hence parity ll b placed in positions 1,2, and 4(startin 4m rite ta left)

compare bits placed in position 3,5,7 for d 1st bit, n set odd/even parity.
similarly 3,6,7th bit for value in 2nd position
and 5,6,7 bit for value in 4th bit

(the bit to compare depends on the binary code of the position. Eg the 1st bit has value 001, hence compare with all bits which ve lsb 1. 2nd bit has binary 010 so compare with all bytes vin 2nd position 1............ this process keeps going 4m left 2 rite)

once the parity bit is set d data is transmitted. i our case the data wuld b calculated as follows
data: 1 0 0 - 1 - -
bit no: 7 6 5 4 3 2 1
by giving parity d data ll be as follows
1000111(odd parity)
this ll b d transmitted data

in case of error say, in 6th bit, ie data received is 1100111, then,
comparing 1,3,5,7 position using the same parity,(odd parity in our case) v get 0(LSB)
comparing 2,3,6,7 position v get 1
comparing 4,5,6,7 position v get 1(MSB)
hence d error in bit no. 110(as calculated above)
n corresponding correction is made

hope this taut yo a bit of hamming code

Next time, check the date that the thread was created before you post a couple hundred lines of badly formatted code which doesn't even work, especially without code-tags to somebody who probably had their problem solved a couple of years ago. Read this.

thanx 4 ur opinion. i ll remember it next tym. well i m new to this . joined sterday only.
i jus ve started learning abt C. thought culd help. sorry anyway
a small hint ta u, next tym if u culd b a bit more polite.

commented: Wrong, we shouldn't have to tell you to read the forum rules, no matter how new you are. -5

did anyone notice.. this thread has been viewed '2,484' times .. strange :) ...

ok i checked most such old threads have been viewed a lot of times .. seems not so strange now ...

Well it'll be three years old soon.

thanx 4 ur opinion. i ll remember it next tym. well i m new to this . joined sterday only.
i jus ve started learning abt C. thought culd help. sorry anyway
a small hint ta u, next tym if u culd b a bit more polite.

English is a rule, too.

commented: Agrees and agrees :) +26
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.