我是编程(一般)和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阵列,然后只输出两个SEPARATE数组。
是否可以创建一个包含总和行和列的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;
}
提前致谢, -Ryan
答案 0 :(得分:0)
通常,数组的使用在 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;
}
}
这会计算所有行的总和,因此您可以引入一个额外的循环来计算列的总和。