C Program to implement hamming code

Written by siddhartha. Posted in Computer Networks

Source Code

/*************************************************************************************/
/*  C Program to implement hamming code.*/
/*  Download more programs at http://sourcecode4u.com/ */
/*************************************************************************************/
#include <stdio.h>
#include <math.h>
#include <string.h>

#define MAXINT 65536
#define SIZE 100

int arr[SIZE][SIZE];
int ele = 0, ind = 0;


void summ(int i)
{
  int j = 0;
  if (isPow2(i))
  {
    arr[ele][ind] = calcPow(i);
    ind++;
  }
  else
  {
    j = findLowerOf2(i);

    arr[ele][ind] = calcPow(j);
    ind++;
    i = i - j;
    summ(i);
  }
}

int isPow2(int i)
{
  if (MAXINT % i == 0)
    return 1;
  return 0;
}

int calcPow(int i)
{
  int count =  - 1;
  if (!isPow2(i))
    printf("flow error...");
  else
  while (i > 0)
  {
    i /= 2;
    count++;
  }

  return count;
}

int findLowerOf2(int i)
{
  int count =  - 1;

  if (isPow2(i))
    return pow(2, i);
  else
  while (i > 0)
  {
    i /= 2;
    count++;
  }


  return pow(2, count);
}

void callSumm(int i)
{
  ind = 0;
  summ(i);
  arr[ele][ind++] =  - 1;
  ele++;

}

void dieError()
{
  exit(1);
}

int howManyTimes(int val, int a[])
{
  int i, count = 0;
  for (i = 0; a[i] !=  - 1; i++)
    if (a[i] == val)
      count++;
  return count;
}

void checkInput(int argc, char str[])
{
  int i = 0;
  if (argc < 2)
  {
    printf("usage: filename.o 'The code string' ");
    printf("ex.a.out 110110");
    dieError();
  }
  for (i = 0; i < strlen(str); i++)
  if (!(str[i] == '0' || str[i] == '1'))
  {
    printf("Please enter a binary string only.....");
    dieError();
  }
}


int calr(int m)
{
  int r = 0;
  for (r = 0; r <= m; r++)
    if (m <= pow(2, r) - 1-r)
      return r;
}

int isEven(int i)
{
  return i % 2 == 0;
}

int main(int argc, char *argv[])
{


  int i, j, k = 0, flag, temp;
  char coded[SIZE];
  int len;
  int m;
  int r;
  int count[SIZE][SIZE];

  checkInput(argc, argv[1]);

  m = strlen(argv[1]);
  r = calr(m);
  len = m + r;


  for (i = 1; i <= len; i++)
    callSumm(i);

  for (j = 0, k = 0; j < r; j++)
  {
    for (i = 0, k = 0; i < len; i++)
      if (howManyTimes(j, arr[i]))
        count[j][k++] = i + 1;

    count[j][k] =  - 1;
  }


  for (i = 0, j = 0; j < len; j++)
  {
    if (!isPow2(j + 1))
      coded[j] = argv[1][i++];
    else
      coded[j] = 'x';

  }

  for (i = 0; i < r; i++)
  {
    for (flag = 0, j = 1; count[i][j] !=  - 1; j++)
    {
      temp = count[i][j] - 1;
      if (coded[temp] == '1')
        flag += 1;
      else if (coded[temp] == '0')
        flag += 0;
    }
    temp = count[i][0] - 1;
    if (isEven(flag))
      coded[temp] = '0';
    else
      coded[temp] = '1';
  }

  printf("The Hamming coded word for your data is");
  for (i = 0; i < len; i++)
    printf("%c", coded[i]);

  char res[100];
  int length_reciver, flag1 = 0;
  printf("\nEnter the length rEcived Hamin code:: ");
  scanf("%d", &length_reciver);
  if (length_reciver != len)
  {
    printf("\n Jaffa Errorrrrrr");
    exit(0);

  }

  printf("\nEnter the recived Hamming code:");
  for (i = 0; i <= length_reciver; i++)
    scanf("%c", &res[i]);


  printf("\nThe lenght of the hamming code:: %d", len);
  printf("\n");
  printf("\nThe haming code recived::");



  for (i = 0; i <= length_reciver; i++)
  {
    printf("%c", res[i]);
  }


  printf("\n This is the haming code::::");
  for (i = 0; i < len; i++)
  {
    printf("%c", coded[i]);
  }

  if (length_reciver != len)
  {
    printf("\nThe reciver code has an error at length it self ha ha ha...");
  }
  else
  {
    for (i = 0; i < len; i++)
    {
      if (res[i + 1] == coded[i])
      {
        printf("\n this is the check::%c ===== %c", res[i + 1], coded[i]);

        flag1 = 0;
      }
      else
      {
        printf("\n  check::%c == %c this is wrong", res[i + 1], coded[i]);
        flag1 = 1;
        printf("The reciver code has an error::%d", i);
        exit(0);
      }
    }
  }
  if (flag1 == 1)
  {
    printf("\n The reciver code has an error::%d", i);
  }
  else
    printf("\n Hurrraa!!!! The reciver code has no error!!");
}

Out Put

BLOG COMMENTS POWERED BY DISQUS