我想创建一个动态二维数组,并希望在其上执行的操作。然而,当我打印的矩阵,第一列显示不同的号码,不知道它在哪儿来的?
我试图让从array[i][j]
到*(*(array+i)+j)
的变化,我还是我得到了相同的结果。
int main ()
{
int sum=0,diagsum=0;
int rowSize,colSize;
std::cout << "Input Row size of the matrix" << '\n';
std::cin >> rowSize;
std::cout << "Input Column size of the matrix" << '\n';
std::cin >> colSize;
int** arrayA = new int*[rowSize]; //declaring array dynamically.
for(int m=0;m<rowSize;++m)
{
arrayA[m] = new int[colSize];
}
//Creating matrix A of size rowSize x colSize with random of first 30
for(int i = 0;i<rowSize;++i)
{
for(int j = 1;j<=colSize;++j)
{
arrayA[i][j]=rand() % 30;
}
}
std::cout << "Matrix A is of size"<< rowSize << " X " << colSize << '\n';
//printing Matrix A
for(int i = 0;i<rowSize;++i)
{
for(int j = 0;j<=colSize;++j)
{
std::cout << arrayA[i][j] << "\t";
}
std::cout << '\n';
}
//sum of element of the matrix
for(int i = 0;i<rowSize;i++)
{
for(int j=0;j<colSize;j++)
{
sum = sum + arrayA[i][j];
}
std::cout << '\n';
}
// sum of elet of diagonal of matirx
for(int i = 0;i<rowSize;i++)
{
diagsum = diagsum + arrayA[i][i];
}
std::cout << "sum of the element of matrix is"<< sum << '\n';
std::cout << "Diagonal sum is" << diagsum <<'\n';
//deleting mem for the array.
for(int m=0;m<1000;m++)
{
delete[] arrayA[m];
}
delete[] arrayA;
return 0;
}
实际结果:
Input Row size of the matrix
5
Input Column size of the matrix
5
Matrix A is of size5 X 5
9140960 11 17 4 10 29
9140960 4 18 18 22 14
9140960 5 5 1 27 1
9140960 11 25 2 27 6
9115176 21 24 2 3 22
sum of the element of matrix is45679273
Diagonal sum is9140974
==================================
预期结果:
Input Row size of the matrix
5
Input Column size of the matrix
5
Matrix A is of size5 X 5
11 17 4 10 29
4 18 18 22 14
5 5 1 27 1
11 25 2 27 6
21 24 2 3 22
最佳答案
您有for(int j = 1;j<=colSize;++j)
的地方应该是for(int j = 0; j < colSize;++j)
。您拥有<= colSize
的任何地方都应该为< colSize
。您有m<1000
的地方应为m < rowSize
,例如
#include <iostream>
int main (void)
{
int sum=0,diagsum=0;
int rowSize,colSize;
std::cout << "Input Row size of the matrix" << '\n';
std::cin >> rowSize;
std::cout << "Input Column size of the matrix" << '\n';
std::cin >> colSize;
int** arrayA = new int*[rowSize]; //declaring array dynamically.
for(int m=0;m<rowSize;++m)
{
arrayA[m] = new int[colSize];
}
//Creating matrix A of size rowSize x colSize with random of first 30
for(int i = 0; i < rowSize; ++i)
{
for(int j = 0; j < colSize; ++j)
{
arrayA[i][j]=rand() % 30;
}
}
std::cout << "Matrix A is of size"<< rowSize << " X " << colSize << '\n';
//printing Matrix A
for(int i = 0;i < rowSize; ++i)
{
for(int j = 0; j < colSize; ++j)
{
std::cout << arrayA[i][j] << "\t";
}
std::cout << '\n';
}
//sum of element of the matrix
for(int i = 0; i < rowSize; i++)
{
for(int j = 0; j < colSize; j++)
{
sum = sum + arrayA[i][j];
}
std::cout << '\n';
}
// sum of elet of diagonal of matirx
for(int i = 0; i < rowSize; i++)
{
diagsum = diagsum + arrayA[i][i];
}
std::cout << "sum of the element of matrix is"<< sum << '\n';
std::cout << "Diagonal sum is" << diagsum <<'\n';
//deleting mem for the array.
for(int m = 0; m < rowSize; m++)
{
delete[] arrayA[m];
}
delete[] arrayA;
return 0;
}
(注意:如果您将循环声明的间隔增加一些,则错误将更容易发现。并且与
++j
或j++
一致)(另请注意:您只需检查
sum
就可以在一个循环中收集diagsum
和if (i == j) diagsum += arrayA[i][j];
)您还应该验证您的用户输入,例如
if (!(std::cin >> rowSize)) {
std::cerr << "error: invalid rowSize\n";
return 1;
}
否则,您将远离未定义行为,只是一键滑倒。
使用/输出示例
$ ./bin/array_dyn_rand
Input Row size of the matrix
5
Input Column size of the matrix
5
Matrix A is of size5 X 5
13 16 27 25 23
25 16 12 9 1
2 7 20 19 23
16 0 6 22 16
11 8 27 9 2
sum of the element of matrix is355
Diagonal sum is73
您应该从计算
std::cout << '\n';
的循环中删除sum
,这完全没有必要。由于您使用的是来自C
rand()
的stdlib.h
,因此您必须在第一次调用srand()
之前通过调用rand()
来为随机数生成器提供种子,通常是使用自纪元以来的秒数进行初始化。#include <ctime>
...
srand (time(NULL)); /* seed the random number generator */
(注意:C ++提供了自己的Pseudo-random number generation例程)
只需对代码进行一些整理以提高可读性,并将
sum
和diagsum
计算合并到一个循环中,则可以执行以下操作:#include <iostream>
#include <ctime>
int main (void)
{
int sum=0,
diagsum=0,
rowSize,
colSize;
std::cout << "Input Row size of the matrix: ";
if (!(std::cin >> rowSize)) { /* validate EVERY user-input */
std::cerr << "error: invalid rowSize\n";
return 1;
}
std::cout << "Input Col size of the matrix: ";
if (!(std::cin >> colSize)) {
std::cerr << "error: invalid colSize\n";
return 1;
}
srand (time(NULL)); /* seed the random number generator */
int **arrayA = new int* [rowSize]; /* allocate rowSize pointers */
for(int m = 0;m < rowSize; m++) /* allocate colSize ints per-row */
arrayA[m] = new int[colSize];
/* Populate rowSize rows x colSize columns with random 0 - 29 */
for (int i = 0; i < rowSize; i++)
for (int j = 0; j < colSize; j++)
arrayA[i][j]=rand() % 30;
std::cout << "\nMatrix A is of size " << rowSize << " x " << colSize
<< "\n\n";
// printing Matrix A
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++)
std::cout << arrayA[i][j] << "\t";
std::cout << '\n';
}
// sum of elements and diagonal of the matrix
for (int i = 0; i < rowSize; i++) {
for(int j = 0; j < colSize; j++) {
if (i == j)
diagsum += arrayA[i][j];
sum = sum + arrayA[i][j];
}
}
std::cout << "\nsum of the element of matrix is: "<< sum << '\n'
<< "Diagonal sum is: " << diagsum <<'\n';
// deleting mem for the array.
for (int m = 0; m < rowSize; m++)
delete[] arrayA[m]; /* delete storage for rows */
delete[] arrayA; /* delete pointers */
return 0;
}
修改后的使用/输出
$ ./bin/array_dyn_rand
Input Row size of the matrix: 5
Input Col size of the matrix: 5
Matrix A is of size 5 x 5
24 8 16 5 15
15 13 24 24 11
16 26 11 12 26
20 9 22 9 22
19 3 3 13 0
sum of the element of matrix is: 366
Diagonal sum is: 57
如果您还有其他问题,请告诉我。