Display 4 numbers per line?

My assignment was to print out all the four-digit base-2 numbers the first digit being 0000 and the last being 1111. So I got the code all figured out, I got all the right numbers. The only thing I have to do is set it up that it would display 4 numbers per line in a table. I'm just stuck on that part. Like for example it should look like this:
0000 0001 0010 0011
0100 0101 0110 0111
and so on...
I need this answered by this Sunday at the latest, thank you!

Here's my code:

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

using namespace std;
int main(int argc, char** argv) {
		
	for (int j = 0; j < 2; j++)
	{
		for (int i = 0; i < 2; i++)
		{
			for (int k = 0; k < 2; k++)
			{
				for (int l = 0; l < 2; l++)
				cout << j << i << k << l << " ";
			}
		}
	}
cout << endl;
	return 0;
}
You need to put an endl after the every fourth output, so find where that would be by tracing through your for loops.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using namespace std;
int main(int argc, char** argv) {
		
	for (int j = 0; j < 2; j++)
	{
		for (int i = 0; i < 2; i++)
		{
			for (int k = 0; k < 2; k++)
			{
				for (int l = 0; l < 2; l++)
				cout << j << i << k << l << " ";
				cout << endl;
			}
		}
	}

	return 0;

Okay, I added it, but I'm getting two columns instead of four?
There are four loops, nested inside each other.

The code controlled by j executes two times.
The code controlled by i executes four times.
The code controlled by k executes eight times.
The code controlled by l executes sixteen times.

Currently the line cout << endl; is controlled by k, hence it executes eight times.
try this:

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

using namespace std;
int main(int argc, char** argv) {
		
	for (int j = 0; j < 2; j++)
	{
		for (int i = 0; i < 2; i++)
		{
		cout << endl;          // this is where u should put the cout << endl;
			for (int k = 0; k < 2; k++)
			{
				for (int l = 0; l < 2; l++)
				cout << j << i << k << l << " ";
			}
		}
	}
cout << endl;
system("PAUSE");
return 0;
}
Last edited on
Topic archived. No new replies allowed.