Thread: Number Pattern Problem?

  1. #1
    Registered User
    Join Date
    Mar 2013

    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:
    int num = 1;
    while (num <= 12)
    for (int i=1; i <= 4; i++)
                         cout << num << " ";

    Last edited by Frank1234; 03-25-2013 at 01:14 PM.

  2. #2
    Registered User
    Join Date
    Aug 2010
    Add (outside of the for loop) "cout << endl;" to print a line feed.

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Liverpool UK
    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
    Last edited by rogster001; 03-25-2013 at 02:03 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Mar 2013
    Thanks for your help. I got it. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number pattern program help
    By nsriv in forum C Programming
    Replies: 7
    Last Post: 12-13-2012, 07:55 AM
  2. Need help to slove this bit pattern problem
    By neeraj87 in forum C Programming
    Replies: 23
    Last Post: 09-15-2011, 07:38 AM
  3. odd and even number pattern
    By christianB in forum C Programming
    Replies: 31
    Last Post: 07-18-2011, 01:04 PM
  4. Replies: 3
    Last Post: 12-15-2010, 01:35 AM
  5. Hmm.. Ai? Finding the pattern in number squences?
    By Zeusbwr in forum C++ Programming
    Replies: 8
    Last Post: 04-02-2005, 06:13 PM