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

main()
{
    int k,i,r,n,j,a[100],m,p,b[100],count=0,count1=0,q,w,g,h,l,z,x,x1;
    printf("enter the length of the data word:");
    scanf("%d",&k);
    printf("\nenter the data word:");
    for(i=1;i<=k;i++)
    {
        scanf("%d",&a[i]);  
    }
    r=1;
    while((k+r+1)>pow(2,r))
        r++;
    printf("\n r=%d",r);
    n=k+r;
     j=1; 
     m=0;
     for(i=1; i<=n; i++)
     {
           p=pow(2,m);
           if(i==p)
           {
                b[i]=0;
                m++;
           }
           else
           {
                b[i]=a[j];
                j++;
           }
     }
     printf("\n INTERMEDIATE CODE WORD IS \n");
     for(i=1; i<=n; i++)
           printf("%d",b[i]);
    count=0;
    i=1;

    // for b[1]

    for(z=2i-1;z<=n;z++)
    {
            printf("%d %d \n",z,b[z]);
            if(b[z]==1)
            {
                count++;
            }
            i++;
    }
    if(count%2==0)
        b[1]=0;
    else
        b[1]=1;






    // b[g]; g=2,4,8,....

    i=1;
    for(q=1;q<=n;q++)    
    {   
        i=1;
        count1=0;
        g=pow(2,q);
        h=pow(2,(q-1));
        printf("%d %d",g,h);
        for(x=h*((4*i)-2);x<=n;x++)
        {
            printf("%d %d",i,x);
            x1=x;
            for(l=x;l<x1+g;l++)
            {
                printf("%d %d",l,x);
                if(b[x]==1)
                    count1++;
                x++;
            }
            i++;
            printf("%d %d",i,x);

        }
        if(count1%2==0)
            b[g]=0;
        else
            b[g]=1;
    }



    printf("\nhamming code: "); 
    for(i=1; i<=n; i++)
           printf("%d",b[i]);   
}

What error?

commented: Why is this voted down? He provided no error in op. +9

I am not getting the correct answer. I'm 90% sure thatthe logic is correct. Please trace the program.

here I'm first generating an itermediate code with the parity bits as 0.
then for the 1st bit , i'm checkin 1,3,5,7,... bits for even parity
then for 2nd bit , I'm checking 2,3,6,7,...bits for even parity
then for 4th bit, I'm checking 4,5,6,7,...bits for even parity.

Your usage of one-letter variable names, which are all declared at the very top of your main() function, has your program nearly impossible to read, let alone debug. You need to provide descriptive variable names if you want your stuff to be self explanatory. Do you seriously think it's good practice to have a variable called count and another one called count1? How on earth do you expect not to get those confused? I'm not suprised at all that you have an untrackable bug in your program.

Never mind. I corrected the error. I got the correct answer. Anyways Thank You

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.