This might confuse a bit, but the array
product[numRows][numCols]
is not going to use product[0][numCols] nor product[numRows][0].
That is why in the above code, even though the array is initialized as a 10x10 matrix, you only get to 9*9=81 in the lower right result.
0*n or n*0, although calculated, are never displayed in
1 2 3 4 5 6
|
for (int row = 1; row < numRows; ++row)
{
for (int col = 1; col < numCols; ++col)
cout << product[row][col] << "\t";
cout << endl;
}
|
Increasing the values
1 2
|
const int numRows = 10; // try changing these two values to 13
const int numCols = 10; // you will get to 12*12=144 maximum
|
will also increase the size of the matrix.
If you instead increase the value, as shown below, because you want to get the "10
th" row/column,
1 2 3 4 5 6
|
for (int row = 1; row < numRows + 1; ++row)
{
for (int col = 1; col < numCols + 1; ++col)
cout << product[row][col] << "\t";
cout << endl;
}
|
you will get erroneous results.
This is called the fencepost error, or off-by-one error.
Some compilers warn you about undefined behavior in loop.
Of course, you can display the whole matrix like this:
1 2 3 4 5 6
|
for (int row = 0 ; row < numRows; ++row)
{
for (int col = 0 ; col < numCols; ++col)
cout << product[row][col] << "\t";
cout << endl;
}
|
to include the first row and column of 0...