GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Oct-2008, 21:46
Mayank1512 Mayank1512 is offline
New Member
 
Join Date: Oct 2008
Posts: 1
Mayank1512 is on a distinguished road

Printing 2D matrix in coiled manner


Print a 2D sqaure matrix using a recursive C function in a coiled manner. the function recieves only 2 arguments, the 2d matrix and the order of the matrix.
coiled manner implies the following path is to be taken:
consider the 3X3 matrix
1 2 3
4 5 6
7 8 9

now the output is 5 2 1 4 7 8 9 6 3. starting from the central digit, one step up, then one left, then 2 down, then 2 right, then 3 up and so on.
  #2  
Old 10-Oct-2008, 11:03
Soybeanio Soybeanio is offline
New Member
 
Join Date: Jul 2008
Posts: 8
Soybeanio is on a distinguished road
Wink

Re: Printing 2D matrix in coiled manner


If you are asking how to make a 2D array, you simply use nested while loops. If not, I am sorry I cannot help you.

CPP / C++ / C Code:
void matrix (int x, int y, char letter);

void matrix (int x, int y, char letter)
{
      int c = 0;
      while (y > 0)
      {
            while (c < x)
            {
                   cout << letter;
                   c++;
            }
            c = 0;
            cout << "\n";
            y--;
      }
}
Last edited by LuciWiz : 10-Oct-2008 at 15:17. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #3  
Old 10-Oct-2008, 13:05
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Printing 2D matrix in coiled manner


A recursive solution may look something like this:
CPP / C++ / C Code:
/* this is pseudocode */
function print_inner_first (matrix)
   if size(matrix) == 1 X 1 (be mindful of matrices without an exact center here)
      output the center value
      return to caller
   else 
      call print_inner_first (trim_outer_rim(matrix))
   end if

   print 'outer rim' of matrix starting at top center position
end function
Try to implement some of that and post back with errors you have.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #4  
Old 10-Oct-2008, 22:27
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Printing 2D matrix in coiled manner


This logic is quite difficult to code this under recursion. You try first with normal loop first and once you get the patterns, reimplemented it with recursion.

I hope this help.
  #5  
Old 11-Oct-2008, 06:04
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Printing 2D matrix in coiled manner


Quote:
Originally Posted by Peter_APIIT
This logic is quite difficult to code this under recursion.
That is actually contrary to the truth. This is a much harder problem done iteratively. One can not learn recursion by using iteration first - strictly personal opinion, of course - it is a way of thinking that needs to be explored on its own.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
  #6  
Old 12-Oct-2008, 00:07
Peter_APIIT Peter_APIIT is offline
Regular Member
 
Join Date: May 2007
Location: Malaysia
Posts: 545
Peter_APIIT can only hope to improve

Re: Printing 2D matrix in coiled manner


Although there are big difference between two of them but you can map the logic in your mind in case of iteration.
  #7  
Old 12-Oct-2008, 08:44
L7Sqr L7Sqr is offline
Member
 
Join Date: Jul 2005
Location: constant limbo
Posts: 234
L7Sqr is a jewel in the roughL7Sqr is a jewel in the rough

Re: Printing 2D matrix in coiled manner


Quote:
Originally Posted by Peter_APIIT
Although there are big difference between two of them but you can map the logic in your mind in case of iteration.
Using the classic example of calculating the gcd of two numbers, how would the iterative approach help map the recursive algorithm in ones mind?
CPP / C++ / C Code:
//recursive
int gcd(int x, int y)
{
  if (y == 0)
     return x;
  else
     return gcd(y, x % y);
}

//iterative
int gcd(int x, int y)
{
    while (y != 0) {
        int remainder = x % y;
        x = y;
        y = remainder;
    }
    return x;
}
The logic of the process must exist outside of the implementation in order to derive the code in the first place, so the code will not help explain the problem.
This is more evident when you start talking about recursive data structures such as trees. Using an iterative approach to implement find in an octtree will certainly not lead to any better understanding to the equivalent recursive one.
__________________
My personal site: Utilities for text processing, debugging, testing and plotting
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiply matrix (2 x 2)* with * as the power resnealc C++ Forum 10 15-Nov-2007 09:11
Printing to dot matrix printer Lem0nHead C Programming Language 3 22-Mar-2007 12:09
i need help in C++ PLZ its_me C++ Forum 3 04-Dec-2006 22:51
Printing an MxN matrix kinkajou C Programming Language 4 06-Nov-2006 12:00
Combining Vectors and References Frankg C++ Forum 7 14-Jan-2006 07:17

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:26.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.