Professional Documents
Culture Documents
Hamming Code 2
Hamming Code 2
Hamming Code 2
h>
#include<math.h>
#include<string.h>
void summ(int i)
{
/* The method to initialize contents of global array by power of 2's sum for
each entry */
int j=0; //index no
if(isPow2(i))
{
/*input no i is of power of 2's form like 1,2,4,8,16...*/
arr[ele][ind]=calcPow(i);
/*calculate what's the power of 2 , in the no, & store to global
array*/
ind++;
}
else
{
/*input no i is NOT of power of 2's form */
j=findLowerOf2(i);
/* try to find just last no , which is of form of power of 2 */
arr[ele][ind]=calcPow(j);
/* again calculate the power*/
ind++;
i=i-j; /* differnce in the no & the last no which is of form of power
of 2 */
/*now call method recursively for the new no (i=i-j) */
summ(i);
}
}
int isPow2(int i)
{
/*if input no is power of two retrun 1 , else 0*/
if(MAXINT % i==0)
return 1; //true
return 0; //false
}
int calcPow(int i)
{
/*Thism ethod returns , what is power of 2 , in a no.
which is of form 2 to the power p */
/* return p , from input of format 2^p */
int count=-1;
/* validate */
if(!isPow2(i))
printf("flow error...
");
else
while(i>0)
{
i/=2;
count++;
}
return count;
}
int findLowerOf2(int i)
{
/*a function to calculate the no , JUST below i , which is power of 2 */
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)
{
/* A method to call summ() method , with assertion that
all global parameters are incremented at each call to summ()
*/
ind=0;
summ(i);
arr[ele][ind++]=-1;
ele++;
}
void dieError()
{
/* If failure , exit the program*/
exit(1);
}
int calr(int m)
{
/*Method to calculate checksum bits for
given m , databits
*/
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;
}
/* validate input */
checkInput(argc,argv[1]);
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;
}
/* Now **********
Frame all equations & solve them
& fill entries in coded table accordingly
*/
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';
}