#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    38
    Rep Power
    3

    Star Output Using Recursion


    I'm trying to write a program that outputs stars using recursion. I cannot use loops.

    For example, if a user inputted the number three, the
    program would look like this

    *
    **
    ***
    **
    *

    Here is my code

    Code:
    #include <iostream>
    using namespace std;
    
    int starPrinter(int howManyStars, int index)
    {
    
        if (index <= howManyStars)
        {
            cout <<"*";
            return starPrinter(howManyStars, ++index);
        }
        else if (index)
        {
            cout <<endl;
        }
    
    }
    
    int starPrinterBackward(int howManyStars, int index)
    {
    
        if (index == howManyStars)
        {
            cout <<endl;
            return starPrinter(howManyStars, --index);
        }
        else if (index)
        {
            cout <<"*";
        }
    
    }
    
    
    void starOutput(int height, int index)
    {
        if (index < height)
        {
            starPrinter(index,0);
            starOutput(height, ++index);
            starPrinterBackward(height, --index);
        }
    
    }
    
    int main()
    {
        int height;
        cout <<"Enter height" <<endl;
        cin >> height;
        starOutput(height, 0);
    
    }
    I'm pretty sure there's something wrong with my "starPrinterBackward" function, but I can't figure out exactly what. It should be similar to my "starPrinter" function, right? Only that it does things in reverse. What I'm thinking is, the backward function needs to start at one less than the user input and go until it reaches one.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2009
    Posts
    45
    Rep Power
    8
    Read warnings that the compiler is printing:
    warning: control reaches end of non-void function
    You declared functions to return int value but some of your if..else blocks lack return statement. These functions only print some characters, they don't need to return any value.

    In your example user inputs 3 but 5 rows are printed so the parameter is actually a width - not height.

    Here's my version of your code (with some modifications).
    Code:
    #include <iostream>
    using namespace std;
    
    void starPrinter(int howManyStars)
    {
        if (howManyStars > 0)
        {
            cout << '*';
            starPrinter(howManyStars - 1);
        }
        else
        {
            cout << '\n';
        }
    }
    
    void starPrinterForward(int howManyStars, int index = 0)
    {
        if (index < howManyStars)
        {
            starPrinter(index + 1);
            starPrinterForward(howManyStars, index + 1);
        }
    }
    
    void starPrinterBackward(int howManyStars, int index = 0)
    {
        if (index < howManyStars)
        {
            starPrinter(howManyStars - index);
            starPrinterBackward(howManyStars, index + 1);
        }
    }
    
    void starOutput(int width)
    {
        if (width > 0)
        {
            starPrinterForward(width);
            starPrinterBackward(width - 1);
        }
    }
    
    int main()
    {
        int width;
        cout << "Enter width\n";
        cin >> width;
        starOutput(width);
        return 0;
    }

IMN logo majestic logo threadwatch logo seochat tools logo