I want to create a dynamic 2d array and want to perform the operation on it. However, when I print the matrix, the first column shows different numbers, and don't know where is it coming from?
I tried to make the change from array[i][j]
to *(*(array+i)+j)
, and still I am getting the same result.
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;
}
Actual result:
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
===================================
Expected result:
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
Where you have for(int j = 1;j<=colSize;++j)
it should be for(int j = 0; j < colSize;++j)
. Everywhere you have <= colSize
should be < colSize
. Where you have m<1000
, should be m < rowSize
, eg
#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;
}
( note: if you space your loop declartions a bit more, the errors will be easier to spot. Also be consistent with either ++j
or j++
)
( also note: you can collect sum
and diagsum
in a single loop, just by checking if (i == j) diagsum += arrayA[i][j];
)
You should also validate your user input, eg
if (!(std::cin >> rowSize)) {
std::cerr << "error: invalid rowSize\n";
return 1;
}
Otherwise, you are one slip of the keyboard away from undefined behavior.
Example Use/Output
$ ./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
You should remove std::cout << '\\n';
from your loop computing the sum
it is completely unnecessary.
Since you are using rand()
from C stdlib.h
, you must seed the random number generator by calling srand()
before your 1st call to rand()
, generally initialized with the seconds since epoch with, eg
#include <ctime>
...
srand (time(NULL)); /* seed the random number generator */
( note: C++ provides it own Pseudo-random number generation routines)
Simply cleaning up your code a bit for readability and combining the sum
and diagsum
calculations in a single loop, you could do:
#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;
}
Revised Use/Output
$ ./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
Let me know if you have further questions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.