如何"bind"对现有矩阵的列和行求和的数组?

How to "bind" arrays that sum columns and rows of existing matrix?

本文关键字:求和 数组 和的 bind 如何      更新时间:2023-10-16

我对编程(一般来说)和c++(特别是)是新手。我正在使用数组并尝试执行以下操作:

1) create a 12x 12 array of (pseudo) random numbers
2) add a 13th row that sums up the columns
3) add a 13th column that sums up the rows
4) add a number to the diagonal of the matrix (e.g. 1 in [R1][C1], 2 in [R2][C2], etc)
5) output the adjusted matrix in step 4

我基本上能够"破解"这个(尽管绝对不是最优雅的方式)。对于上面的# 2和# 3,我使用一个12x12的数组,然后只输出两个单独的数组。

是否有可能创建一个13x13矩阵,包括行和列的总和?是否有可能使用指针来简化这一点?

如果是这样,有人能指出我在正确的方向(我没有经验与他们合作)?

代码如下:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int N = 12;
const int M = 12;
int Matrix_M[N][M] = {0};
int rowSum[N] = {0};
int colSum[M] = {0};
void generateArray();
void Parity();
void Average();
void addError();
int main()
{
    generateArray();
    Parity();
    addError();
    Parity();
    return 0;
}
void generateArray()
{
    // 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;
    // 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_M[i][j] = rand() % 100;
            // outputs the raw matrix
            cout << left << setw(4) << Matrix_M[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl << endl;
}
void Parity()
{
    cout << "The parity values are:" << endl << endl;
    // 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;
    cout << left << setw(12) << "Columns" << left << setw(12) << "Rows" << endl;
    cout << left << setw(12) << "-------" << left << setw(12) << "----" << endl;
    // sums the row and column elements
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; ++j)
        {
            rowSum[i] += Matrix_M[i][j];
            colSum[i] += Matrix_M[j][i];
        }
        // outputs the sums
        cout <<  setw(5) << colSum[i];
        cout << left << setw(65) << rowSum[i] << endl;
    }
}

void addError()
{
    // some explanatory text
    cout << endl << endl << endl;
    cout << "The following matrix introduces an error along the diagonal" << endl;
    // 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;
    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_M[i][j] += (i+1)*1;
            }
            // outputs the matrix containing errors 
            cout << left << setw(4) << Matrix_M[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl << endl;
}

提前感谢,对

一般来说,数组的使用在 c++ 中是不使用的。例如,使用std::vector代替:

const int N = 12;
const int M = 12;
std::vector<int> matrix = std::vector<int>((N+1)*(M+1),0);    //creating the matrix adding one extra row and column for the sums;
//filling the matrix
for(int ii=0; ii<N; ii++)
{
    for(int jj=0; jj<M; jj++)
    {
        matrix[ii*(M+1) + jj] = rand() % 100;
        matrix[ii*(M+1) + M] += matrix[ii*(M+1) + jj];    //updating the sum;
    }
}

这将计算所有行的和,因此您可以引入一个额外的循环来计算列的和。