To print the pattern of the following using c/c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VaibhavKashyap
    New Member
    • May 2013
    • 4

    To print the pattern of the following using c/c++

    how to print
    43
    12
    for 2X2 matrix and
    543
    612
    789
    for a 3X3 matrix

    and like the pattern continues the matrix shd hv the numbers of its square

    for example as the user gives in 4 and 4

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

    Thanks in advance
    Vaibhav
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Hi Vaibhav and welcome to bytes.com!

    This sounds very much like a homework question, so we won't give you a solution (as stated in our posting guidelines). We will however happily help you find one yourself. What have you done so far? What have you found out about the problem? Do you understand the pattern they are printed in?
    Last edited by Nepomuk; May 21 '13, 03:02 PM. Reason: Added a welcome

    Comment

    • VaibhavKashyap
      New Member
      • May 2013
      • 4

      #3
      How???

      Originally posted by Nepomuk
      Hi Vaibhav and welcome to bytes.com!

      This sounds very much like a homework question, so we won't give you a solution (as stated in our posting guidelines). We will however happily help you find one yourself. What have you done so far? What have you found out about the problem? Do you understand the pattern they are printed in?
      Actually I understood the pattern that those are numbers of the square from 1 to x square;;

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        Yes, they are those numbers. But there's also an order to the numbers; do you understand that order? Why for example is the 2x2 matrix
        Code:
        4 3
        1 2
        rather than, say
        Code:
        1 2
        3 4
        ?

        As soon as you've understood the order, I would probably create a 2D-Array and fill it with the numbers, calculating the position of every number before putting it into the array. For this, I'm sure you'll be using the % and / operators, also you may want to use some method of saving the direction you're currently "moving". (This is only one possible solution, you could calculate the position independently for every number.)

        Comment

        • VaibhavKashyap
          New Member
          • May 2013
          • 4

          #5
          understood

          Originally posted by Nepomuk
          Yes, they are those numbers. But there's also an order to the numbers; do you understand that order? Why for example is the 2x2 matrix
          Code:
          4 3
          1 2
          rather than, say
          Code:
          1 2
          3 4
          ?

          As soon as you've understood the order, I would probably create a 2D-Array and fill it with the numbers, calculating the position of every number before putting it into the array. For this, I'm sure you'll be using the % and / operators, also you may want to use some method of saving the direction you're currently "moving". (This is only one possible solution, you could calculate the position independently for every number.)

          ya I understood the pattern but how do I position the numbers

          Comment

          • Nepomuk
            Recognized Expert Specialist
            • Aug 2007
            • 3111

            #6
            You're welcome. :-)

            Comment

            • VaibhavKashyap
              New Member
              • May 2013
              • 4

              #7
              reply

              Originally posted by Nepomuk
              You're welcome. :-)
              hey how do I position the numbers????in 2d array

              Comment

              • Nepomuk
                Recognized Expert Specialist
                • Aug 2007
                • 3111

                #8
                I take it you're asking how to use a 2d array? If so, here's a very basic example:
                Code:
                #include<stdio.h>
                
                int** array;
                
                int main() {
                	array = (int**) malloc(sizeof(int) * 2 * 2);
                	array[0] = (int*) malloc(sizeof(int) * 2);
                	array[1] = (int*) malloc(sizeof(int) * 2);
                
                	array[0][0] = 4;
                	array[0][1] = 3;
                	array[1][1] = 2;
                	array[1][0] = 1;
                
                	printf("%d %d\n", array[0][0], array[0][1]);
                	printf("%d %d\n", array[1][0], array[1][1]);
                }
                Or did I misunderstand your question?

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  I suggest specifying a field width in the printf format string to insure your numbers are properly aligned. For example,
                  Code:
                       printf("%2d %2d\n", array[0][0], array[0][1]);

                  Comment

                  Working...