Hamming Code DCN

This program is for implementing Hamming Code. This is a part of Mumbai University MCA Colleges Data Communication and Networking MCA Sem 4



#include<stdio.h>
#include<conio.h>
#include<math.h>

int d[11],s[11],r[11],c[11];
int p1,p2,p4,p8;
int n,i,j,ctr;
void Sender();
void Receiver();
void ham(int []);
void main()
{

            int ch;
            clrscr();
            printf("\n\n1. SENDER");
            printf("\n2.RECEIVER");
            printf("\n3. END");
            while(1)
            {
                        printf("\n\nEnter your choice : ");
                        scanf("%d",&ch);
                        switch(ch)
                        {
                                    case 1:
                                    Sender();
                                    break;

                                    case 2:
                                    Receiver();
                                    break;

                                    case 3:
                                    exit();
                        }
            }
            getch();
}


void ham(int d[])
{
            ctr=0;
            for(i=0;i<11;i++)
            {
                        if(i==0 || i==2 || i==4 || i==6 || i==8 || i==10)
                        {
                                    if(d[i]==1)
                                                ctr++;
                        }
            }
            if(ctr%2==0)
                        p1=0;
            else
                        p1=1;
            ctr=0;

            for(i=0;i<11;i++)
            {
                        if(i==1 || i==2 || i==5 || i==6 || i==9 || i==10)
                        {
                                    if(d[i]==1)
                                                ctr++;
                        }
            }
            if(ctr%2==0)
                        p2=0;
            else
                        p2=1;
            ctr=0;

            for(i=0;i<11;i++)
            {
                        if(i==3 || i==4 || i==5 || i==6)
                        {
                                    if(d[i]==1)
                                                ctr++;
                        }
            }
            if(ctr%2==0)
                        p4=0;
            else
                        p4=1;
            ctr=0;

            for(i=0;i<11;i++)
            {
                        if(i==7 || i==8 || i==9 || i==10)
                        {
                                    if(d[i]==1)
                                                ctr++;
                        }
            }
            if(ctr%2==0)
                        p8=0;
            else
                        p8=1;
            ctr=0;

            d[0]=p1;
            d[1]=p2;
            d[3]=p4;
            d[7]=p8;
            for(i=0;i<11;i++)
                        s[i]=d[i];

}


void Sender()
{
            printf("\n\nEnter Binary data of 7 bits : ");
            for(i=0;i<11;i++)
            {
                        if(i==0 || i==1 || i==3 || i==7)
                        {}
                        else
                                    scanf("%d",&s[i]);
            }
            printf("\n\nData : ");
            for(i=0;i<11;i++)
                        printf("%d",s[i]);

            ham(s);
            printf("\n\nHamming Data : ");
            for(i=0;i<11;i++)
                        printf("%d",s[i]);

}

void Receiver()
{
            int e;
            printf("\n\nEnter Binary data of 11 bits : ");
            for(i=0;i<11;i++)
                        scanf("%d",&r[i]);
            for(i=0;i<11;i++)
                        c[i]=r[i];

            printf("\n\nData : ");
            for(i=0;i<11;i++)
                        printf("%d",r[i]);

            ham(r);
            if(p1==0 && p2==0 && p4==0 && p8==0)
                        printf("\n\nThe Received Data is Right.");
            else
            {
                        e=0;
                        if(p1==1)
                                    e=e+pow(2,0);
                        if(p2==1)
                                    e=e+pow(2,1);
                        if(p4==1)
                                    e=e+pow(2,2);
                        if(p8==1)
                                    e=e+pow(2,3);
                        printf("\n\nThe Received Data has Some ERROR.");
                        printf("\n\nThe ERROR is at position : %d",e);
                        if(c[e-1]==1)
                                    c[e-1]=0;
                        else
                                    c[e-1]=1;

                   printf("\nThe Corrected Data : ");
            for(i=0;i<11;i++)
                        printf("%d",c[i]);
            }
}



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a Comment