Code:
#include <stdio.h>
#define ROWS 4
#define COLS 8
int main()
{
int i,j,k,temp;
int matrix[ROWS][COLS]={{0,1,0,1,1,0,0,1},{1,1,1,0,0,1,0,0},{0,0,1,0,0,1,1,1},{1,0,0,1,1,0,1,0}};
int opMatrix[ROWS][COLS];
int countArray[ROWS]={0}; //this array is used for knowing how many number of 1's present in each row of matrix
int fArray[ROWS]={0};
int receivedCodeWord[COLS]={1,1,0,1,0,1,0,1};
int originalCodeWord[COLS];
int dout[COLS];
int onecount = 0,zerocount = 0;
//initializing the opMatrix to -1
for(i=0;i<ROWS;i++)
for(j=0;j<COLS;j++)
opMatrix[i][j]=-1;
printf("H Matrix is: \n\n");
for(i=0;i<ROWS;i++)
{
for(j=0;j<COLS;j++)
printf("%d\t",matrix[i][j]);
printf("\n");
}
for(i=0;i<ROWS;i++)
{
temp = 0;
for(j=0;j<COLS;j++)
{
if(matrix[i][j] == 1)
{
opMatrix[i][temp++] = j;
}
}
countArray[i] = temp;
}
for(i=0;i<ROWS;i++)
{
int temp,val;
if(countArray[i] == 1)
{
temp = opMatrix[i][0];
fArray[i]=receivedCodeWord[temp];
}
else
{
temp = opMatrix[i][0];
val = receivedCodeWord[temp];
for(j = 1;j<countArray[i];j++)
{
temp = opMatrix[i][j];
temp = receivedCodeWord[temp];
val = val ^ temp;
}
fArray[i] = val;
}
}
for(i=0;i<COLS;i++)
originalCodeWord[i] = receivedCodeWord[i];
for(k=0;k<COLS;k++)
{
originalCodeWord[k] == 0 ? zerocount++ : onecount++;
for(i=0;i<ROWS;i++)
{
int flag = 0; //to know whether that particular c is connected to f
for(j=0;j<COLS;j++)
{
if(opMatrix[i][j]==k)
{
flag = 1;
break;
}
}
if(flag == 1)
{
temp = fArray[i];
int temp2 = originalCodeWord[k];
if(temp == 1)
{
temp2 = temp2 ^ 1;
}
temp2==0?zerocount++:onecount++;
}
}
if(zerocount>onecount)
{
originalCodeWord[k] = 0;
}
else
{
originalCodeWord[k] = 1;
}
originalCodeWord[k] = 1;
}
printf("OriginalCodeWord is: \n\n");
for(i=0;i<COLS;i++)
{
dout[i] =originalCodeWord[i];
printf("%d ",dout[i]);
}
return 0;
}
Please anyone help me