如何识别和纠正"奇偶校验位"在矩阵?

时间:2014-07-17 18:54:50

标签: c++ matrix

我是编程(一般)和C ++(特别是)的新手。我正在研究一个问题,要求我以下列方式使用ARRAYS:

  1. 生成一个12x12的伪随机数矩阵(我们称之为'原始'矩阵)
  2. 添加第13列,表示行的总和(这是“奇偶校验”)
  3. 添加第13行,表示列的总和(这是“奇偶校验”)
  4. 在该矩阵的对角线上添加一个错误(我选择将1加到[R1] [C1],2加到[R2] [C2],3加到[R3] [C3]等)... (我们将此矩阵称为“调整后的矩阵”)
  5. 检测调整后的矩阵中的错误并进行纠正
  6. 我能够做到1-4,但是在概念化和整合#5时遇到了问题。我认为它看起来像这样:

    a)“如果调整后的矩阵中第1列的总和不等于原始矩阵中第1列的总和,则取总和之间的差值”

    b)“如果调整后的矩阵中第1行的总和不等于原始矩阵中第1行的总和,则取总和之间的差值”

    c)“如果总和之间的这两个差异是相同的,那么在矩阵中找到WHERE,它会偏离1(例如,在[R1] [C1]中),并从交叉点中包含的值中减去该值。调整后的矩阵中的行/列“

    有人可以提供一些关于如何将a,b和c概念化并合并到我的代码中的指导吗?

    我的代码是:

    #include "stdafx.h"
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    
    using namespace std;
    
    const int N = 12;
    const int M = 12;
    
    int matrix[N][M] = { 0 };
    //int *matrix_ptr[N];  // pointer to the rows
    //int *matrix_ptr[M];  // pointer to the columns
    //int **matrix_prt_ptr = &matrix_ptr[0];  // pointer to the pointer
    
    int rowSum[N] = { 0 };
    int colSum[M] = { 0 };
    
    void RainGen();
    void Parity();
    void Average();
    void addError();
    void errCorr();
    void outputColumnHeaders();
    
    int main()
    {
        RainGen();
        Parity();
        Average();
        addError();
        Parity();
    
        return 0;
    }
    
    void RainGen()
    {
        // some explanatory text
        cout << "\n"
            << "The following matrix displays rainfall over the past 12 years" << endl;
    
        outputColumnHeaders();
    
        // sets the seed for the number generator
        unsigned setSeed = 1023;
        srand(setSeed);
    
        // generates the matrix using pseudo-random numbers
    
        // fill the rows first
        for (int i = 0; i < N; i++)
        {
    
            // the fill the columns
            for (int j = 0; j < M; j++)
            {
                matrix[i][j] = rand() % 100;
    
                // outputs the raw matrix
                cout << left << setw(4) << matrix[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl << endl;
    }
    
    void Parity()
    {
        cout << "This matrix has parity values of:" << endl;
    
        outputColumnHeaders();
    
    
        // sums the row and column elements
    
        // sums the rows
        for (int i = 0; i < N; i++)
        {
            rowSum[i] = { 0 }; //
            for (int j = 0; j < M; ++j)
            {
                rowSum[i] += matrix[i][j];
            }
    
            // outputs the sums
            cout << left << "                                                            " <<
                setw(5) << rowSum[i] << endl;
        }
    
        // sums the columns
        for (int i = 0; i < N; i++)
        {
            colSum[i] = { 0 }; //
            for (int j = 0; j < M; ++j)
            {
                colSum[i] += matrix[j][i];
            }
    
            // outputs the sums
            cout << setw(5) << colSum[i];
        }
    
        cout << "\n";
    
    }
    
    void Average()
    {
        cout << endl << endl
            << "The average rainfall by month is:" << endl;
    
        outputColumnHeaders();
    
        // computes the average rainfall by month
        for (int i = 0; i < N; i++)
        {
            colSum[i] = { 0 };
            for (int j = 0; j < M; ++j)
            {
                colSum[i] += matrix[j][i];
            }
            cout << setw(5) << colSum[i] / M;
        }
    }
    
    void addError()
    {
        // some explanatory text
        cout << endl << endl << endl;
        cout << "The following matrix introduces an error along the diagonal:" << endl;
    
        outputColumnHeaders();
    
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < M; j++)
            {
                // introduces an error to the previously-generated number in the original matrix
                // adds 1 to [R1][C1], adds 2 to [R2][C2], adds 3 to [R1][C1] ... adds 12 to [R12][C12]
                if (i == j)
                {
                    matrix[i][j] += (i + 1) * 1;
                }
    
                // outputs the matrix containing errors 
                cout << left << setw(4) << matrix[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl << endl;
    }
    
    void outputColumnHeaders()
    {
        // generates the column headers (months of the year)
    
        cout << endl << endl;
        cout << left << setw(5) << "Jan"
            << left << setw(5) << "Feb"
            << left << setw(5) << "Mar"
            << left << setw(5) << "Apr"
            << left << setw(5) << "May"
            << left << setw(5) << "Jun"
            << left << setw(5) << "Jul"
            << left << setw(5) << "Aug"
            << left << setw(5) << "Sep"
            << left << setw(5) << "Oct"
            << left << setw(5) << "Nov"
            << left << setw(5) << "Dec" << endl;
    
        cout << left << setw(5) << "---"
            << left << setw(5) << "---"
            << left << setw(5) << "---"
            << left << setw(5) << "---"
            << left << setw(5) << "---"
            << left << setw(5) << "---"
            << left << setw(5) << "---"
            << left << setw(5) << "---"
            << left << setw(5) << "---"
            << left << setw(5) << "---"
            << left << setw(5) << "---"
            << left << setw(5) << "---" << endl;
    }
    

    非常感谢您提前看一看。 --Ryan

0 个答案:

没有答案