arrayscparity

Parity bit check, within an array


I am writing a program in the C language which is supposed to check the even-horizontal-bit parity of an Binary data array.

The array is created by a different program part. The important point is that it generates an 8x8 array. This array then gets passed to the ParityCreation() function.

The program works as follows. It takes over an 8x8 data array, and expand it to an 8x9 array. So that there is an extra column for the parity bits, of each row. Next, while iterating through the rows, the sum of non-zero row elements is computed and stored under the iNonZero variable. After that I check whether iNonZero is even or odd. If it is odd, the value 1 is written into the last column of that row. If it is even a 0 is written instead.

The problem is according to my understanding the program should for the following matrix:

{{0, 1, 0, 0, 0, 0, 0, 1},
 {0, 1, 0, 0, 1, 0, 0, 1},
 {0, 1, 0, 1, 0, 0, 1, 1},
 {0, 0, 1, 1, 0, 1, 0, 0},
 {0, 1, 0, 0, 1, 1, 0, 0},
 {0, 1, 0, 0, 1, 0, 0, 1},
 {0, 1, 0, 1, 0, 1, 1, 0},
 {0, 1, 0, 0, 0, 1, 0, 1}};

Give out the this matrix:

{{0, 1, 0, 0, 0, 0, 0, 1, 0},
 {0, 1, 0, 0, 1, 0, 0, 1, 1},
 {0, 1, 0, 1, 0, 0, 1, 1, 0},
 {0, 0, 1, 1, 0, 1, 0, 0, 1},
 {0, 1, 0, 0, 1, 1, 0, 0, 1},
 {0, 1, 0, 0, 1, 0, 0, 1, 1},
 {0, 1, 0, 1, 0, 1, 1, 0, 0},
 {0, 1, 0, 0, 0, 1, 0, 1, 1}};

However it doesn't. It fills the last column with zeros and the lower right corner with an arbitrary number.

I suspect this has something to do with the iNonZero not working i.e. not resetting at the right point. But I am unsure. The code is as follows:

//EVEN PARITY MATRIX
void ParityCreation(int iBitMatrix[][8])
{
    int i = 0;
    int j = 0;
    int iNonZero = 0;

    //Neue Marix erstellen
    int iParitaetsMatrix[8][9];

    //Writing the iBitMatrix into the iParitaetsMatrix
    for(i = 0; i <= 7; i++)
    {
        for(j = 0; j <= 8; j++)
        {
            iParitaetsMatrix[i][j] = iBitMatrix[i][j];
        }
    }

    //Looping thorough the rows of the Matrix
    for (i = 0; i <= 8; i++)
    {
        //Setting the iNoneZero to 0, so that it is reseted for each iteration
        iNonZero = 0;
        //Looping through the columns of the Matrix
        for (j = 0; j <= 9; j++)
        {
            if (iBitMatrix[i][j] != 0)
                iNonZero += 1;
        }

        //Add a parity bit "1" if number of non-zero elements is odd
        if(iNonZero % 2 != 0)
        {
            iBitMatrix[i][9] = 1;
        }

        //Add a parity bit "0" if number of non-zero elements is even
        else //(iNonZero % 2 == 0)
        {
            iBitMatrix[i][9] = 0;
        }
    }

    //Output of the horizontal parity bit:
    int iColumns = 0;
    int iRows = 0;

    printf("\n\n\n");
    for(iColumns=0; iColumns<=7; iColumns++)
    {
        for(iRows=0; iRows<=8; iRows++)
        {
            printf(" %i " ,iParitaetsMatrix[iColumns][iRows]);
        }
    printf("\n");

    }
}


//Driver code
int main()
{
    int iBitMatrix[8][8] = {{0, 1, 0, 0, 0, 0, 0, 1},
                            {0, 1, 0, 0, 1, 0, 0, 1},
                            {0, 1, 0, 1, 0, 0, 1, 1},
                            {0, 0, 1, 1, 0, 1, 0, 0},
                            {0, 1, 0, 0, 1, 1, 0, 0},
                            {0, 1, 0, 0, 1, 0, 0, 1},
                            {0, 1, 0, 1, 0, 1, 1, 0},
                            {0, 1, 0, 0, 0, 1, 0, 1}};

    ParityCreation(iBitMatrix);
    return 0;
}

It would be very helpful if you could say where the error is. Because I am unable to find it, or maybe my logic is faulted.


Solution

  • Part of the issue in your code is you're going beyond the elements in your matrix. for (i = 0; i <= 8; i++) that should be < 8 because 0-7 and for (j = 0; j <= 9; j++) should be < 9 because 0-8. The rest is just a bit of cleanup. Also it's better to define constants instead of using magic numbers.

    void ParityCreation(int iBitMatrix[8][8])
    {
        //create the matrix;
        int nParityMatrix[8][9] = {0};
        
        //assign to the previous matrix
        for(int nRow = 0; nRow < 8; ++nRow){
            
            int nParity = 0;
            
            for(int nCol = 0; nCol < 8; ++nCol){
                nParityMatrix[nRow][nCol] = iBitMatrix[nRow][nCol];
                
                //check the parity
                if(iBitMatrix[nRow][nCol] == 1){
                    if(nParity == 0){
                        nParity = 1;
                    }else{
                        nParity = 0;
                    }
                }
            }
            //assign the parity
            nParityMatrix[nRow][8] = nParity;
        }
    
        
        for(int nRow = 0; nRow < 8; ++nRow){
            for(int nCol = 0; nCol < 9 ; ++nCol){
                printf("%d ",nParityMatrix[nRow][nCol]);
            }
            printf("\n");
        }
    }
    
    
    //Driver code
    int main()
    {
        int iBitMatrix[8][8] = {{0, 1, 0, 0, 0, 0, 0, 1},
                                {0, 1, 0, 0, 1, 0, 0, 1},
                                {0, 1, 0, 1, 0, 0, 1, 1},
                                {0, 0, 1, 1, 0, 1, 0, 0},
                                {0, 1, 0, 0, 1, 1, 0, 0},
                                {0, 1, 0, 0, 1, 0, 0, 1},
                                {0, 1, 0, 1, 0, 1, 1, 0},
                                {0, 1, 0, 0, 0, 1, 0, 1}};
    
        ParityCreation(iBitMatrix);
        return 0;
    }
    

    You also don't need multiple loops to do the initial copying then checking parity. It can be done in one loop.