for (int i = 1; i < ro; i++) {
mb[0][i] = i;
}
for (int i = 1; i < ro; i++){
mb[i][0] = i;
for (int j = 1; j < col; j++)
mb[i][j] = i * j;
}
The mb[0][i] = i and mb[i][0] = i will add the headers to the mb array.
However, in your latest code you need to change your loops on lines 11, 13, 30, 33 to start at 0, and continue while i < ro and j < col. The way your loops are now will mean you access data beyond the end of the array - hence the weird numbers.
And you don't need to output i & j on line 36.
#include <iostream>
#include <iomanip>
usingnamespace std;
constint ro=6;
constint col=6;
int mb[ro][col];
void multi()
{
for (int i = 1; i <ro; i++) {
mb[0][i]=i;
}
for (int i = 1; i <ro; i++){
mb[i][0]=i;
for (int j = 1; j < col; j++){
mb[i][j]= i * j;
}
}
}
int main()
{
multi();
for (int i = 0; i <ro; i++) {
for (int j =0 ; j <col; j++) {
cout<<" " << setw(4) << mb[i][j];
}
cout << endl;
}
system("pause");
return 0;
}
I noticed that when my i and j started and 0 in the multi function i would get 0's on my first row and col, so i changed it to 1. and added 1x1 row and column to the table.
can you explain to me what was that I did that made it work?:D
and how the code would've look with your suggestion? thanks!
You know, you fixed some issues that I didn't even notice when I was looking at it before, so +1 to you ;) The biggest thing was (as you pointed out), an extra column & row needed to be added to the array in order to store the column & row headers.
As for how you fixed it, well I would explain the operation of your code by stepping through it and working out in my mind how the program is altering the mb array at each line:
line 7 - when you first declare the array it is default-initialised with all 0 values
(because it is defined at the global scope)
mb = 0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
line 11 - this for loop sets the first element in each column (at row index 0) equal to
the column number (which is the also the array index) - i.e. this is our column
header
mb = 0 1 2 3 4 5
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
- However, note that this loop starts at 1, so mb[0][0] is not overwritten in this
loop. Luckily for us, the default value that it was initialised to is 0, which is
what we wanted.
- For best practice, it would probably be a good idea to start this loop at 0, so
that we explicitly overwrite all of the elements in the first row.
line 16 - this for loop is looping through all of the rows in the mb array, except for the
first one. This is good because we don't want to overwrite the header values that
we just wrote into the first row with the loop at line 11
line 18 - this sets the first element in each row (at column index 0) equal to the row
number - i.e. this is our row header
- the rest of the multi function does all of the other calculations & puts them in
the correct place in the mb array
The above is a bit long-winded (I got tired towards the end), but that's the kind of process I'd normally go through.