How can I show this output on screen?

I want to write a program that makes this output to appear on screen using for-loop :
0        0
         1
         2
         3
         4
         5

1        0
         1
         2
         3
         4
         5

2        0
         1
         2
         3
         4
         5

3        0
         1
         2
         3
         4
         5 


I can't seem to make the correct logic/engine of this nested loop. Anyone help.
Last edited on
Have you tried? Let's see the code ...
Yep I have tried but it comes like this :
0        
         0
         1
         2
         3
         4
         5

1        
         0
         1
         2
         3
         4
         5

2       
         0
         1
         2
         3
         4
         5

3        
         0
         1
         2
         3
         4
         5


And not like :
0        0
         1
         2
         3
         4
         5

1        0
         1
         2
         3
         4
         5

2        0
         1
         2
         3
         4
         5

3        0
         1
         2
         3
         4
         5


Here is the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
	for (int a=0 ; a<=3 ; a++)
	{
		cout<<a<<endl;
		for (int b=0; b<=5 ; b++)
		{
			cout<<"         "<<b<<endl;
		}
	}

	return 0;
}


Please let me know of any modifications.
Last edited on
You need to get rid of the endl after a.

Think about this logically. You want to output a and then output b and then AFTER THAT have a newline. Try your putting your newline in a place that achieves this.

//output a
//output b
//newline
//output b
//newline
//etc
Good! Excellent! Try this:

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
#include <iostream>

using std::cout;

int main()
{
	for(int a = 0; a < 4; ++a)
		{
			cout << a;
			for(int b = 0; b < 6; ++b)
				{
					if(b == 0)
						cout << "        " << b << '\n';
					else
						cout << "         " << b << '\n';
				}
			cout << "\n";
		}
}
0        0
         1
         2
         3
         4
         5

1        0
         1
         2
         3
         4
         5

2        0
         1
         2
         3
         4
         5

3        0
         1
         2
         3
         4
         5


oh yes finally i have found out that.
it's something like this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
	for (int a=0 ; a<=3 ; a++)
	{
		cout<<a;
		for (int b=0; b<=5 ; b++)
		{
			cout<<"\t"<<b<<endl;
		}
		cout<<endl;
	}

	return 0;
}


But one thing that i still can't understand is with "\t". This \t means some amount of spaces(tab) 4 or 5 i don't remember. But when I replace this with manual " "(spaces in between), it gives me slightly misplaced output.

Example :-
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

using namespace std;

int main()
{
	for (int a=0 ; a<=3 ; a++)
	{
		cout<<a;
		for (int b=0; b<=5 ; b++)
		{
			cout<<"     "<<b<<endl;
		}
		cout<<endl;
	}

	return 0;
}


Output:
0         0
         1
         2
         3
         4
         5

1         0
         1
         2
         3
         4
         5

2         0
         1
         2
         3
         4
         5

3         0
         1
         2
         3
         4
         5


Why this happens?
Or, slightly changed:

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
#include <iostream>
#include <iomanip>

using std::cout;
using std::setw;

int main()
{
	for(int a = 0; a < 4; ++a)
		{
			for(int b = 0; b < 6; ++b)
				{
					if(b == 0)
						cout << a << setw(8) << b << '\n';
					else
						cout << setw(9) << b << '\n';
				}
			cout << "\n";
		}
}
0       0
        1
        2
        3
        4
        5

1       0
        1
        2
        3
        4
        5

2       0
        1
        2
        3
        4
        5

3       0
        1
        2
        3
        4
        5

Thanks condor, that was impressive and simple ( what a dumbass i had been). But could you please answer my question mentioned above?
Yes.
line 9 in your last code cout << a occupies a space and when "appears" b from line 12 will be shifted. Then loop with b goes on without that "a" till the end of the loop b and so on.

EDIT: If all OK please mark this thread as Solved.
Last edited on
But why this doesn't happen with "\t" ? Do you know that?
Last edited on
cout << "\t"; counts the occupied spaces from the very beginning of the line ignoring the presence of that "a".
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
#include <iostream>

using std::cout;

int main()
{
	for(int a = 0; a < 4; ++a)
		{
			cout << a;
			for(int b = 0; b < 6; ++b)
				cout << '\t' << b << '\n';
			cout << '\n';
		}
}
0	0
	1
	2
	3
	4
	5

1	0
	1
	2
	3
	4
	5

2	0
	1
	2
	3
	4
	5

3	0
	1
	2
	3
	4
	5
Last edited on
Okay. My problem is solved. Thanks man. Have a nice day!
Topic archived. No new replies allowed.