C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 29th, 2012, 11:32 PM
anonymousme anonymousme is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2012
Posts: 38 anonymousme User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 46 m 22 sec
Reputation Power: 1
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.

Reply With Quote
  #2  
Old November 30th, 2012, 02:35 AM
DRK82 DRK82 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 25 DRK82 User rank is Corporal (100 - 500 Reputation Level)DRK82 User rank is Corporal (100 - 500 Reputation Level)DRK82 User rank is Corporal (100 - 500 Reputation Level)DRK82 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 2 h 40 m 42 sec
Reputation Power: 0
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;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Star Output Using Recursion

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap