[英]Getting weird output for dynamic allocation of the 2d array. How can I resolve the issue?
我想创建一个动态2D数组,并要对其执行操作。 但是,当我打印矩阵时,第一列显示不同的数字,并且不知道它来自哪里?
我试图将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++
)
( 还请注意:您可以通过检查if (i == j) diagsum += arrayA[i][j];
diagsum
在一个循环中收集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 stdlib.h
rand()
,因此您必须在第一次调用rand()
之前通过调用srand()
来播种随机数生成器,通常使用从纪元开始的秒数进行初始化,例如
#include <ctime>
...
srand (time(NULL)); /* seed the random number generator */
( 注意: C ++提供了它自己的伪随机数生成例程)
只需对代码进行一些整理以提高可读性,并将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
如果您还有其他问题,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.