# Number Pattern Problem?

• 03-25-2013
Frank1234
Number Pattern Problem?
Hello Guys; I wrote a pattern program so the output would come like this: 1 2 3 4
5 6 7 8
9 10 11 12 (where there is 4 numbers in each line), but right now the output comes out like this: 1 2 3 4 5 6 7 8 9 10 11 12(where numbers are right after another). could you help me figuring out how to change my program. Thanks guys. My program right now is:
Code:

int num = 1;
while (num <= 12)
{
for (int i=1; i <= 4; i++)
{
cout << num << " ";
num++;
}
}

• 03-25-2013
kmdv
Add (outside of the for loop) "cout << endl;" to print a line feed.
• 03-25-2013
rogster001
what is your overall goal, why do you want the right alignment? You could do this if you store your numbers in an array, work out the largest number and thus the maximum width, ie number of digits in a 'field' based on the biggest value input and then output your numbers relative to this width and relative to each number you intend to output.

So if the biggest number in the range is 1500 then your max field width is 4 chars
Then if the next number to output is >= 1000 you just output it followed by your usual " " delimeter
if the next number is >= 100 you first output a single " " followed by number followed by delimeter
if next number is >= 10 you output two spaces " " number then delimeter
if next number is < 10 and >= 0 you output three spaces, number then delimeter

Working around something like that will do it as a simple roll your own method, dont know if anything exists to do it for you in the console

1000 , 100, 10, 0 can be constants to help you scale it - you will also need a maximum allowable value to help you define the constants and validate the input

obviously this output depends on only allowing unsigned integers, unless you also check for minus values and implement a version that accomodates the extra char that will be displayed should minus numbers be found in the range
• 03-25-2013
Frank1234
Thanks for your help. I got it. Thanks.