Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 13th, 2006, 1:26 PM   #1
cairo
Newbie
 
Join Date: Dec 2005
Posts: 18
Rep Power: 0 cairo is on a distinguished road
Unhappy problem with printstar

void printStar(int input)
{
    if (input >=0 )
    {
        printStar(input-1);      
       for(int i=1; i<=input; i++)
       {
               cout <<"*";
               }
       cout<<endl;
        
       }  
         
   }


i try to print the stars below with recursion, but i failed

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

i would appreciate, if i got hint from u all
cairo is offline   Reply With Quote
Old Apr 13th, 2006, 4:15 PM   #2
a3orange
Newbie
 
Join Date: Apr 2006
Posts: 14
Rep Power: 0 a3orange is on a distinguished road
Do you see this line:

void printStar(int input)
{
    if (input >=0 )
    {
        printStar(input-1);       
       for(int i=1; i<=input; i++)
       {
               cout <<"*";
               }
       cout<<endl;
        
       }  
         
   }

You keep recalling the function which makes it impossible to begin the for loop. You would probably want to do something like this..


void printStar(int input)
{
    if (input >=0 )
    {
           
       for(int i=1; i<=input; i++)
       {
               cout <<"*";
               }
       printStar(input-1);   
       cout<<endl;
        
       }  
         
   }

Edit: Wait, did you want to print this:

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

If so, you're going to have to do some more stuff.
a3orange is offline   Reply With Quote
Old Apr 13th, 2006, 6:01 PM   #3
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
must disagree with you there, the code correction you posted is incorrect, this is not how recursion is done. in regards to the problem, the approach is correct, you have called the function recursively till you reach 0 then print line by line form 1 start up to 'input' stars .. this only gives half the figure unfortunately, i cant think of a a way to do it recursively. but if you want the end product to be
*
**
***
***
**
*
then just do a for-loop for the bottom part in main
for (int i = input ; i >=1 ; i--)
hbe02 is offline   Reply With Quote
Old Apr 13th, 2006, 6:59 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
void printStar(int count, int limit)
{
    for (int i = 0; i < limit-count; i++) cout << '*';
    cout << endl;
    if (count ) printStar (count-1, limit);
    for (int i = 0; i < limit-count; i++) cout << '*';
    cout << endl;
}
int main (int argc, char *argv [])
{
    printStar (5, 5);
    puts ("Press ENTER to terminate\n");
    getchar ();
    return 0;
}
Quote:
Originally Posted by output

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

Press ENTER to terminate
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 14th, 2006, 10:39 AM   #5
cairo
Newbie
 
Join Date: Dec 2005
Posts: 18
Rep Power: 0 cairo is on a distinguished road
thanks u all guys, i got a clue from dawei
thanks a lot ^^
cairo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:25 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC