如何"bind"对现有矩阵的列和行求和的数组?
How to "bind" arrays that sum columns and rows of existing matrix?
我对编程(一般来说)和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;
}
}
这将计算所有行的和,因此您可以引入一个额外的循环来计算列的和。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 使用动态数组求和和和求平均值
- 平衡数组索引,同时从左和右对数组求和
- C++ 如何从文本文件中扫描二维数组并对行求和?
- 动态规划 - 在数组中查找目标求和方式
- 数组中的"Maximum Sum mod M"范围:求和和计数
- 数组的并行求和比C++中的顺序求和慢
- 在 c++ 中对 2D 数组列求和
- 如何在c++中对2d数组中的列求和
- 用于从包含最大求和的整数数组中提取子数组的算法
- 循环访问 CUDA 中的三维数组以对它们的元素求和
- 我如何确定我是否可以将数组中的某些元素求和为 K
- 求和除以4的数组数
- CUDA/推力:如何对交错数组的列求和
- C++将字符串数组转换为整数,然后求和int数组
- 从文件中读取数据到2d数组中,并使用c++对特定数组求和
- 如何使用本函数对两个字符数组进行元素相乘,并将相乘结果求和为int
- 如何"bind"对现有矩阵的列和行求和的数组?
- 返回不正确值的数组的顺序求和
- c++,最快的STL容器,用于递归地执行{delete[begin], insert[end]和对整个数组内容求和}