Thread: print c pattern

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    9

    print c pattern

    Please help in printing the foll pattern, m not able to do so
    I am giving 2 egs:
    if n=4

    1 2 3 4
    9 10 11 12
    13 14 15 16
    5 6 7 8

    or if n=5

    1 2 3 4 5
    11 12 13 14 15
    22 23 24 25 26
    16 17 18 19 20 21
    6 7 8 9 10
    Last edited by surbhijain93; 03-11-2015 at 10:28 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,007
    The problem with these kinds of patterns is that there are possible, perhaps very complex, pattern rules that were not anticipated by the question setter, but which could fit the given examples. That said, one could apply Occam's razor and look for a simple pattern: what such pattern do you observe in the examples? Describe it in words.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    9
    The first line contains 1 to n numbers and in total there are n lines. After the first line next n nos are in last line . After that next n nos are printed in 2nd line and then second last line and so on...

  4. #4
    Registered User
    Join Date
    Oct 2014
    Posts
    9

  5. #5
    Registered User
    Join Date
    Oct 2014
    Posts
    9
    pls smeone tell why there is runtime error?

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    746
    Quote Originally Posted by surbhijain93 View Post
    pls smeone tell why there is runtime error?
    The only (Compile time) error that I encountered was the definition of main().

    main() should always return an int.

    Without the need for command line arguments, it should be defined as
    Code:
    int main(void)
    {
        /* all code */
    
        return 0;
    }
    Or with command line argument suport:

    Code:
    int main(int argc, char *argv[])
    {
    
        /* All code */
    
        return 0;
    }
    Please always post your code here in CODE blocks, as I have done here, and please indent your code properly.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    3,906
    Quote Originally Posted by surbhijain93 View Post
    Please help in printing the foll pattern, m not able to do so
    I am giving 2 egs:
    if n=4

    1 2 3 4
    9 10 11 12
    13 14 15 16
    5 6 7 8

    or if n=5

    1 2 3 4 5
    11 12 13 14 15
    22 23 24 25 26
    16 17 18 19 20 21
    6 7 8 9 10
    Are you sure the second one is NOT supposed to be?

    Code:
    Ignore this Line
     1  2  3  4  5
    11 12 13 14 15
    21 22 23 24 25
    16 17 18 19 20
     6  7  8  9 10
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    746
    Quote Originally Posted by stahta01 View Post
    Are you sure the second one is NOT supposed to be?

    Code:
    Ignore this Line
     1  2  3  4  5
    11 12 13 14 15
    21 22 23 24 25
    16 17 18 19 20
     6  7  8  9 10
    Tim S.
    See the original post. (Unless the OP copied the assignment incorrectly.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-13-2012, 12:34 PM
  2. bit pattern
    By hell0 in forum C Programming
    Replies: 3
    Last Post: 07-25-2011, 08:38 AM
  3. Replies: 10
    Last Post: 02-19-2010, 07:50 PM
  4. Replies: 0
    Last Post: 03-28-2003, 08:20 AM
  5. How do I print a pattern flush right?
    By Basia in forum C Programming
    Replies: 5
    Last Post: 06-11-2002, 07:15 AM