Good old Number Patterns

Hello all. This is my first post in this forum and I hope I won't sound like a complete idiot.
I need a little hint for this number pattern problem I have here. I have to print the following pattern -:
1 _ _ _ _ _ _ 1
2 2 _ _ _ _ 2 2
3 3 3 _ _ 3 3 3
4 4 4 4 4 4 4 4


So far I've gotten the left side numbers and the underscores right, but I just can't think of how to get the right side numbers. My code looks something like this -:

1
2
3
4
5
6
7
8
9
    int i, j, k;
    for(i = 1; i <= 4; i++)
    {
          for(j = 1; j <= i; j++)
             cout << i;
          for(k = 1; k <= 2*4 - i; k++)
             cout << "_";
          cout << endl;
    }

For which I get the following output -:
1 _ _ _ _ _ _ _
2 2 _ _ _ _ _ _
3 3 3 _ _ _ _ _ 
4 4 4 4 _ _ _ _ 


I thought of doing the right side numbers by the concept of a right-lower triangular matrix, but I can't get that right either. Then I thought of starting i from the maximum value and decrementing thereon but that would screw up everything else. What do I do, senors?

(Note : Spaces between underscores and numbers are only shown for clarity)
Last edited on
i'th line is : i times 'i', some '_' and i times 'i' again.
length of every line is 8 = i + some + i, thus some = 8-2*i
you however consider it = 8-i.
Thanks for the quick reply, hamsterman. With your help, I've figured it out...it's much, much simpler than how I saw it :)

1
2
3
4
5
6
7
8
9
10
11
for(i = 1; i <= 4; i++)
    {
          for(j = 1; j <= 8; j++)
          {
                if(j > i && j <= 8 - i)
                   cout << "_";
                else
                   cout << i;
          }
          cout << endl;
    }
Last edited on
Topic archived. No new replies allowed.