Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何";绑定";对现有矩阵的列和行求和的数组? 我对编程(一般)和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_C++_Arrays_Pointers - Fatal编程技术网

如何";绑定";对现有矩阵的列和行求和的数组? 我对编程(一般)和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

如何";绑定";对现有矩阵的列和行求和的数组? 我对编程(一般)和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,c++,arrays,pointers,C++,Arrays,Pointers,我基本上能够“破解”这个(虽然肯定不是以最优雅的方式)。对于上面的#2和#3,我使用一个12x12数组,然后只输出两个单独的数组 是否可以创建一个包含行和列总和的13x13矩阵? 是否可以使用指针来简化此过程? 如果是这样,有人能给我指出正确的方向吗(我没有与他们合作的经验)? 代码如下: #include "stdafx.h" #include <iostream> #include <cstdlib> #include <iomanip> using n

我基本上能够“破解”这个(虽然肯定不是以最优雅的方式)。对于上面的#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;
}
#包括“stdafx.h”
#包括
#包括
#包括
使用名称空间std;
常数int N=12;
常数int M=12;
int矩阵μM[N][M]={0};
int rowSum[N]={0};
int colSum[M]={0};
void-generatarray();
无效奇偶性();
无效平均值();
void addError();
int main()
{
generatarray();
奇偶性();
加法器();
奇偶性();
返回0;
}
void生成器array()
{
//生成列标题(一年中的月份)

cout通常数组的使用在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;
    }
}
const int N=12;
常数int M=12;
std::vector matrix=std::vector((N+1)*(M+1),0);//创建矩阵,为求和添加一个额外的行和列;
//填充矩阵

对于(int ii=0;iiI)来说,我知道向量是一种方法。但是,从技术上讲,我们还没有了解它们。有没有一种方法可以使用数组和指针来实现这一点?当然,只需对数组执行相同的操作:
int矩阵[(N+1)*(M+1)]={0}
。以下步骤是相同的。谢谢,这非常有用。当我输出No时,我不认为
矩阵M[I*j]
是您想要输出的。如果
I
表示行号,
j
列,您可以使用
矩阵M[I*(M+1)+j]打印出相应的元素
if
M
是没有最后一个求和列的列数。或者,您可以使用
int Matrix_M[N+1][M+1]={0}
并通过
Matrix_M[i][j]访问行
i
和列
j
中的元素。