# Thread: recursive function

1. ## 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. 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. 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;

}```

4. 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. @jdm: that wasn't a recursive function. don't forget that your method is just as important, if not moreso, than the final result...

6. Welcome to the boards Colie.

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

gg

7. 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. >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?

9. hmm teach yourself in 21 days

10. >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.

11. 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. >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.

Popular pages Recent additions