For-loops

How could I get a program to generate something like this? It displays 1 and 2 the same increasing amount of times until it gets to six.

121122111222111122221111122222111111222222

I feel like this should be really easy but I can't quite place what I need to do. I would think a for-loop would work here but I might be wrong.
Thanks!
Last edited on
You need to use a nested for loop, the end result would look like this:

1
2
3
4
5
6
7
for(int i = 0; i < 6; i++)
{
   for(int j = 0; j < i +1; i++)
   {
       //output
   }
}
An alternative, yet quite likely worse solution is:
1
2
3
4
5
6
7
8
9
10
11
   int i = 1;
   int j = 2;

   int k = 1;
   while (k < 6) {
        for (int l = 1; l < k + 1; l++)
            cout << i;
        for (int m = 1; m < k + 1; m++)
            cout << j;
        k++;
   }

@Above Posters: Don't just post code, it generally helps more if you just try to give tips so that the OP can discover it on their own.
closed account (D80DSL3A)
How about this silly method? It needs just one for loop!
@pastamaker: This is probably NOT how you are supposed to do it (for your assignment).
@firedraco: Sorry, but it looks like the cat's out of the bag already, plus it's a goofy method.
1
2
3
4
5
6
7
8
9
char ones[7], twos[7];
for(int k=1; k<=6; ++k)
{
	ones[k-1] = '1';// write another '1' to the array
	ones[k] = '\0';// move the string end forward
	twos[k-1] = '2';// likewise
	twos[k] = '\0';
	cout << ones << twos;
}
Topic archived. No new replies allowed.