Thread: Printing Patterns Using Recursion

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    37

    Printing Patterns Using Recursion

    Hi guys,

    I'm trying to print patterns such as the following:

    ****
    ***
    **
    *
    **
    ***
    ****

    using recursion. I can figure out the code for the top half and the bottom half seperately. i.e i can print

    ****
    ***
    **
    *

    and

    *
    **
    ***
    ****

    each recursively. I'm trying to combine these into one function. How could I call the functions so that I could combine the two?

    here are my functions

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    
    
    void printTriangle (int n)
    {
     if (n==1)
     {printf("*");
      printf("\n");
     }
     
     else if(n>1)
     {   int i;
         for (i=0;i<n;i++)
         printf("*");
         printf("\n");
         printTriangle(n-1);
     }
    }
    
    void printInvertedTriangle(int n)
    {
        if (n==1)
     {printf("*");
      printf("\n");
     }
     
     else if(n>1)
     {
    
         printInvertedTriangle(n-1);
         int i;
         for (i=0;i<n;i++)
         printf("*");
         printf("\n");
     }
    }
    
    
    
    int main(void)
    {printInvertedTriangle(4);
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    This isn't really a problem where recursion would do any good. If you know how to print the triangles for each number n i give you all you have to do is printTriangle(n) and printInvertedTriangle(n) and you are done. What do you want to use recursion for?

    Recursion should be avoided as much as possible and only used when it makes sense because it is computationally expensive. For each function call you make there is so much additional overhead done that it just doesn't make sense to use it for such a trivial exercise.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    37
    I know, I was going through some exercises at the back of a book I have. Most of them are easily solvable using loops. I was thinking this exercise was trying to show a way of doing recursion that I don't know how to do, or something like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED homework help! Printing out patterns.
    By omGeeK in forum C Programming
    Replies: 8
    Last Post: 09-28-2010, 07:16 PM
  2. Replies: 17
    Last Post: 09-21-2009, 09:50 PM
  3. Printing Patterns
    By ferniture in forum C Programming
    Replies: 11
    Last Post: 11-17-2008, 10:00 PM
  4. Printing a centric square using recursion
    By ChaosTheMatador in forum C Programming
    Replies: 1
    Last Post: 03-07-2006, 06:13 PM
  5. printing a btree using recursion
    By agerealm in forum C++ Programming
    Replies: 2
    Last Post: 02-22-2003, 08:57 AM