help me finish this code pls i just need headers

Hi i made this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  #include <iostream>
#include <iomanip>
using namespace std;

const int ro=5;
const int col=5;
int mb[ro][col];

void multi()
{
for (int i = 0; i < 5; i++) {
	
for (int j = 0; j < 5; j++){

	mb[i][j]= i * j;


}
}
}


int main()
{


multi();


for (int i = 0; i < 5; i++) {

	
for (int j = 0; j < 5; j++) {

cout << "   " << setw(4) << mb[i][j];

}
cout << endl;
}
system("pause");
return 0;

} 


it works well but i also need to include headers for the table for example the output should look like this
1
2
3
4
5
6
7
 
0 1 2  3   4  5
1 1 2  3   4  5
2 2 4  6   8  10
3 3 6  9   12 15
4 4 8  12  16 20
5 5 10 15  20 25

should i need another for loop? thanks
Last edited on
No need for another loop. Just print the header at line 29.
there is nothing at line 29 ;/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 #include <iostream>
#include <iomanip>
using namespace std;

const int ro=5;
const int col=5;
int mb[ro][col];

void multi()
{
for (int i = 1; i <= ro; i++) {
	
for (int j = 1; j <= col; j++){

	mb[i][j]= i * j;


}
}
}


int main()
{


multi();


for (int i = 1; i <= ro; i++) {

	
for (int j = 1; j <= col; j++) {

	
cout <<  i <<  j <<"   " << setw(4) << mb[i][j];

}
cout << endl;
}
system("pause");
return 0;

} 
something like this? it gives me weird numbers tho
The multi function in your original code on your previous post ( http://www.cplusplus.com/forum/beginner/146875/ ) was correct
i.e.
1
2
3
4
5
6
7
8
9
10
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.
Last edited on
so this is my code now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
using namespace std;

const int ro=5;
const int col=5;
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 = 1; i <= ro; i++) {
	
for (int j = 1; j <= col; j++) {
	
cout <<" "<< setw(4) << mb[i][j];


}
cout << endl;
}
system("pause");
return 0;

} 


but it only prints 5 rows no headers D: I think Im missing something
So close! All of your loops should start at 0 and exit when i == ro or col
i.e.
for (int i = 0 ; i < ro ; i++)
and the same for the j loops.
Hi again. I think i solved the problem by trial and error lol. I'm not sure if you suggested me to do this but here it goes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <iomanip>
using namespace std;

const int ro=6;
const int 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!
Me:
No need for another loop. Just print the header at line 29.

Dksam123:
there is nothing at line 29 ;/

Exactly. My point is that you should add code at line 29 to print a heading.
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.

And this is a useful resource for understanding more about arrays (I've certainly found it useful in the past): http://www.cplusplus.com/doc/tutorial/arrays/

Hope that helps!
Topic archived. No new replies allowed.