Saturday, February 1, 2020

2D Parity Bit check and correction implementation using C

Hey there all, since I could not find any suitable 2D parity check technique implementation in C across the web, I decided to build my own :), the code is a bit redundant but works perfectly according to the books. The code might be perfect for your university assignment..

ALGORITHM

1) START
2) Receive input from the user for number of bits per stream.
3) Receive the input from the user bit by bit ( Here we are using 4 streams of bits).
4) Store them in a double array using 2 for loops
5) Check for row parity and if the number of 1s are even store 0 in the sender side row parity array otherwise store 1 in the array
6) Repeat step 3-5 for the receiver
7) Run a for loop to counter check the elements of the 4 arrays, and if they don't match, store the position of row parity array in one variable and position of column parity array in another.
8) Display the result.
9) END


CODE

#include<stdio.h>

int main()
{
    int i,n,frame,j,sender[100][100],paritysendrow[10],paritysendcol[10],receiver[100][100],count,parityrecrow[10],parityreccol[10];
    int rowpos,colpos;
    printf("Enter the number of bits per stream:");
    scanf("%d",&n);
    printf("\n*Enter the message to be sent bit by bit*");
    for(i=0;i<4;i++)
    {
        printf("\nEnter Stream %d: ",i+1);

        for(j=0;j<n;j++)
    {
        scanf("%d",&sender[i][j]);
    }
    }



    for(i=0;i<4;i++)
    {
        count=0;
        for(j=0;j<n;j++)
        {
            if(sender[i][j]==1)
            {
                count++;
            }
        }
        if(count%2==0)
        {
            paritysendrow[i]=0;
        }
        else
            paritysendrow[i]=1;
    }

     for(i=0;i<n;i++)
    {
        count=0;
        for(j=0;j<4;j++)
        {
            if(sender[j][i]==1)
            {
                count++;
            }
        }
        if(count%2==0)
        {
            paritysendcol[i]=0;
        }
        else
            {
            paritysendcol[i]=1;
            }
    }
    printf("The matrix built is:");

    for(i=0;i<4;i++)
    {
        printf("\n");
        for(j=0;j<n;j++)
        {
            printf("%d ",sender[i][j]);
        }

        }

    printf("\nRow wise Parity:\n");
    for(i=0;i<n;i++)
    {
        printf("%d ",paritysendrow[i]);
    }
    printf("\nCol wise parity:\n");

    for(i=0;i<n;i++)
    {
        printf("%d ",paritysendcol[i]);
    }

    printf("\n*Enter the message Received Bit by bit*");
    for(i=0;i<4;i++)
    {
        printf("\nEnter Stream %d: ",i+1);

        for(j=0;j<n;j++)
    {
        scanf("%d",&receiver[i][j]);
    }
    }

     printf("The matrix built is:");

    for(i=0;i<4;i++)
    {
        printf("\n");
        for(j=0;j<n;j++)
        {
            printf("%d ",receiver[i][j]);
        }

        }

        for(i=0;i<4;i++)
    {
        count=0;
        for(j=0;j<n;j++)
        {
            if(receiver[i][j]==1)
            {
                count++;
            }
        }
        if(count%2==0)
        {
            parityrecrow[i]=0;
        }
        else
            parityrecrow[i]=1;
    }
     for(i=0;i<n;i++)
    {
        count=0;
        for(j=0;j<4;j++)
        {
            if(receiver[j][i]==1)
            {
                count++;
            }
        }
        if(count%2==0)
        {
            parityreccol[i]=0;
        }
        else
            {
            parityreccol[i]=1;
            }
    }

    printf("\nRow wise Parity:\n");
    for(i=0;i<n;i++)
    {
        printf("%d ",parityrecrow[i]);
    }
    printf("\nCol wise parity:\n");

    for(i=0;i<n;i++)
    {
        printf("%d ",parityreccol[i]);
    }

  for(i=0;i<n;i++)
  {
      if(paritysendrow[i]!=parityrecrow[i])
      {
          rowpos=i;
      }
  }
   for(i=0;i<n;i++)
  {
      if(paritysendcol[i]!=parityreccol[i])
      {
          colpos=i;
      }
  }

  printf("\nThe Error is in %d ROW ",rowpos+1);
  printf("%d COL",colpos+1);

}

SCREENSHOT





THANK YOU !!

No comments:

Post a Comment

2D Parity Bit check and correction implementation using C

Hey there all, since I could not find any suitable 2D parity check technique implementation in C across the web, I decided to build my own ...