Home > Mobile >  Recursive version of drawPyramid() function
Recursive version of drawPyramid() function

Time:10-11

I am a newbie to C. I tried to implement a serial drawPyramid function. Now, I want to implement a recursive version of this function drawPyramid_rec for my own practice. However, I am stuck for hours. No ideas how to deal with the leading whitespaces in each row... I felt like somehow I have to store the value of n in the first recursive call. Or perhaps there is no possibility to implement a recursive version of drawPyramid? Please help!

#include <stdio.h>

void drawPyramid(int n);

int main(void)
{
    int n;
    do
    {
        printf("Height: ");
        scanf("%i", &n);
    }
    while (n > 8 || n < 1);

    drawPyramid(n);

    return 0;
}

void drawPyramid(int n)
{
    for (int height = 1; height <= n;   height)
    {
        for (int column = (n - height); column >= 1; --column)
        {
            printf(" "); // putchar(' ');
        }

        for (int column = 1; column <= height;   column)
        {
            putchar('#'); // printf("#");
        }

        printf("  ");

        for (int column = 1; column <= height;   column)
        {
            printf("#");
        }

        printf("\n");
    }
}

Output:

Height: 5
    #  #
   ##  ##
  ###  ###
 ####  ####
#####  #####

CodePudding user response:

I felt like somehow I have to store the value of n in the first recursive call.

Yes, you have to preserve the value of n, which is the height of the pyramid. To do that, you could add an extra parameter to your function drawPyramid that never changes it.

void drawPyramid_recursive(int n, int height)
{
    if (height == 0) // base case
    {
        return;
    }

    drawPyramid_recursive(n, height - 1);

    for (int column = (n - height); column >= 1; --column)
    {
        printf(" "); // putchar(' ');
    }

    for (int column = 1; column <= height;   column)
    {
        putchar('#'); // printf("#");
    }

    printf("  ");

    for (int column = 1; column <= height;   column)
    {
        printf("#");
    }

    printf("\n");
}

CodePudding user response:

The recursive function can look for example the following way as it is shown in the demonstration program below.

#include <stdio.h>
#include <limits.h>

FILE * drawPyramid( unsigned int n, unsigned int m, FILE *fp )
{
    const char c = '#';

    if ( INT_MAX < n ) n = INT_MAX;

    if ( m < n )
    {
        fprintf( fp, "%*c", n - m , c );
        for ( unsigned int i = 1; i < m   1; i   )
        {
            fputc( c, fp );
        }

        fprintf( fp, "%*c", 3, c );
        for ( unsigned int i = 1; i < m   1; i   )
        {
            fputc( c, fp );
        }
        
        fputc( '\n', fp );

        drawPyramid( n, m   1, fp );
    }

    return fp;
}

int main(void) 
{
    while ( 1 )
    {
        printf( "Enter the height of the pyramid (0 - exit): " );

        unsigned int n;

        if ( scanf( "%u", &n ) != 1 || n == 0 ) break;

        putchar( '\n' );

        drawPyramid( n, 0, stdout );

        putchar( '\n' );;
    }

    return 0;
}

The program output might look like

Enter the height of the pyramid (0 - exit): 1

#  #

Enter the height of the pyramid (0 - exit): 2

 #  #
##  ##

Enter the height of the pyramid (0 - exit): 3

  #  #
 ##  ##
###  ###

Enter the height of the pyramid (0 - exit): 4

   #  #
  ##  ##
 ###  ###
####  ####

Enter the height of the pyramid (0 - exit): 5

    #  #
   ##  ##
  ###  ###
 ####  ####
#####  #####

Enter the height of the pyramid (0 - exit): 6

     #  #
    ##  ##
   ###  ###
  ####  ####
 #####  #####
######  ######

Enter the height of the pyramid (0 - exit): 7

      #  #
     ##  ##
    ###  ###
   ####  ####
  #####  #####
 ######  ######
#######  #######

Enter the height of the pyramid (0 - exit): 8

       #  #
      ##  ##
     ###  ###
    ####  ####
   #####  #####
  ######  ######
 #######  #######
########  ########

Enter the height of the pyramid (0 - exit): 9

        #  #
       ##  ##
      ###  ###
     ####  ####
    #####  #####
   ######  ######
  #######  #######
 ########  ########
#########  #########

Enter the height of the pyramid (0 - exit): 10

         #  #
        ##  ##
       ###  ###
      ####  ####
     #####  #####
    ######  ######
   #######  #######
  ########  ########
 #########  #########
##########  ##########

Enter the height of the pyramid (0 - exit): 0
  • Related