recursive function

This is a discussion on recursive function within the C++ Programming forums, part of the General Programming Boards category; I need help to write a recursive function to generate the following pattern of stars: * * * * * ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    2

    recursive function

    I need help to write a recursive function to generate the following pattern of stars:

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

    I also need write a program to test the recursive function and promts the user to enter the number of lines in the pattern and uses the recursive function to generate the pattern when the user presses enter. For example, the middle line of stars in the pattern above has 4 stars in it. If the user were to enter 5 into the program, then the program would display the following:

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

    If you'll notice, the line in the middle now has 5 stars instead of 4.
    i would greatly appreciate any help.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Here is a hint:
    Code:
    void recurseivefunction(int x)
    {
      // do something
      recurseivefunction(x+1);
      // do something else
    }
    You'll get more help if you show an attempt.

  3. #3
    jdm
    jdm is offline
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    Ok, I'm pretty new to C++ myself, so here's my hackish way of doing it. I'm sure there is a cleaner method, but this is my way. I encourage you to do it your own way, rather than copy my code verbatim.

    Code:
    #include <iostream>
    using namespace std;
    
    int main(void)
    {
    
    	bool flag = 0;
    	int userInput = 0;
    
    	cout << "Please enter a number: ";
    	cin >> userInput;
    	cout << endl << "Your pattern looks like this: " << endl << endl;
    	
    	int count = 1;
    
    	while (count <= userInput && count > 0)
    	{
    		if (!flag)
    		{
    			for (int i = 0; i < count; i++)
    			{
    				cout << "*";
    			}
    			if (count < userInput)
    			{
    				count++;
    			}
    			else
    			{
    				flag = true;
    				count--;
    			}
    			cout << endl;
    		}
    		else
    		{
    			for (int i = 0; i < count; i++)
    			{
    				cout << "*";
    			}
    			count--;
    			cout << endl;
    		}
    	}
    	
    	cout << endl;
    	return 0;
    
    }
    // Visual C++ 6.0... newbie with many questions!

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    1
    Here's a simple program using recursion to generate your pattern:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void DisplayLine(int nCurrentDepth, int nDepth)
    {
      int i;
    
      for(i = 0; i < nCurrentDepth; i++)
        cout << "*";
    
      cout << endl;
    
      if(nCurrentDepth >= nDepth)
        return ;
    
      DisplayLine(nCurrentDepth+1, nDepth);
    
      for(i = 0; i < nCurrentDepth; i++)
        cout << "*";
    
      cout << endl;
    
      return ;
    }
    
    int main(void)
    {
      int   nDepth=0;
    
      cout << "Please enter a number: ";
      cin  >> nDepth;
      cout << endl << "Your pattern looks like this: " << endl << endl;
    
      DisplayLine(0, nDepth);
    
      return 0;
    }

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    @jdm: that wasn't a recursive function. don't forget that your method is just as important, if not moreso, than the final result...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    Welcome to the boards Colie.

    We try not to do folk's homework here....

    gg

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    can i ask a silly question since this is a recursion topic.

    on the book im reading it says, "theres 2 variaties of recursion, those that end and produce an answer and those that never end and produce a runtime failure, programmers think the latter is funny (when it happens to someone else)."

    i fail toget it but why is it funny? is this a bad joke? lol

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is this a bad joke?
    Yes. Clearly the author has a skewed and twisted sense of humor. Witty comments are funny. Silly bugs that we spend hours looking for that turn out to be something painfully obvious are funny (after the fact). A program going down in flames is not funny, and anyone who thinks it is probably doesn't have many friends.

    Out of curiosity, what book did you get that from?
    My best code is written with the delete key.

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    hmm teach yourself in 21 days

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >teach yourself in 21 days
    If you mean the one by Jesse Libery then I'm not surprised. He's not a very good author, and he tries to hide it behind poor humor, irrelevant anecdotes and overly complex code.
    My best code is written with the delete key.

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    yes thats the one, but if what u say is true then why the book was such a big sucess? when i bought the book i was looking for something with a lot of pages, cheap and for beginners, i dont know much about authors or c++ books.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but if what u say is true then why the book was such a big sucess?
    There's no accounting for taste. Herbert Schildt is probably the most successful author of C and C++ books, but they are all only good as doorstops, table risers, and tinder.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By Fork in forum C Programming
    Replies: 3
    Last Post: 10-26-2006, 11:27 AM
  2. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21