簡體   English   中英

數組內的奇偶校驗位檢查

[英]Parity bit check, within an array

我正在用 C 語言編寫一個程序,該程序應該檢查二進制數據數組的偶數水平位奇偶校驗。

該數組由不同的程序部分創建。 重要的一點是它會生成一個 8x8 數組。 然后將該數組傳遞給ParityCreation()函數。

該程序的工作原理如下。 它接管一個 8x8 數據數組,並將其擴展為一個 8x9 數組。 這樣每一行的奇偶校驗位都有一個額外的列。 接下來,在遍歷行時,計算非零行元素的總和並將其存儲在iNonZero變量下。 之后我檢查 iNonZero 是偶數還是奇數。 如果是奇數,則將值 1 寫入該行的最后一列。 如果它甚至是一個 0 則被寫入。

問題是根據我的理解程序應該用於以下矩陣:

{{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}};

給出這個矩陣:

{{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}};

然而事實並非如此。 它用零填充最后一列,用任意數字填充右下角。

我懷疑這與 iN​​onZero 無法正常工作有關,即沒有在正確的位置重置。 但我不確定。 代碼如下:

//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;
}

如果您能說出錯誤在哪里,那將非常有幫助。 因為我找不到它,或者我的邏輯有問題。

您似乎傾向於訪問超出數組大小的數組。 請記住,大小為 N 的數組具有最大的大小索引 (N - 1)。

我還將index <= (N -1)的每個實例更改為index < N ,因為我認為這更清楚。

並且在設置奇偶校驗位時訪問了錯誤的數組。

更正此問題時,新代碼應為:

//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 < 8; i++) //changed
    {
        for(j = 0; j < 8; j++) //changed
        {
            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

        /* here you only want to access the first 8 in stead of all 9, since the 9th will be the parity bit you want to calculate*/
        for (j = 0; j < 8; j++) //changed
        {
            if (iBitMatrix[i][j] != 0)
                iNonZero += 1;
        }

        //Add a parity bit "1" if number of non-zero elements is odd
        if((iNonZero % 2) != 0) //changed for clarity
        {
            iParitaetsMatrix[i][8] = 1; //changed; accessing wrong array and index too big
        }

        //Add a parity bit "0" if number of non-zero elements is even
        else //(iNonZero % 2 == 0)
        {
            iParitaetsMatrix[i][8] = 0; //changed; accessing wrong array and index too big
        }
    }

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

    printf("\n\n\n");
    for(iColumns=0; iColumns < 8; iColumns++) //changed
    {
        for(iRows=0; iRows < 9; iRows++) //changed
        {
            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;
}

您的代碼中的部分問題是您超出了矩陣中的元素。 for (i = 0; i <= 8; i++)應該是< 8因為 0-7 和for (j = 0; j <= 9; j++)應該是< 9因為 0-8。 剩下的只是一些清理工作。 此外,最好定義常量而不是使用幻數。

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;
}

您也不需要多個循環來進行初始復制然后檢查奇偶校驗。 它可以在一個循環中完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM