C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-20-2004, 11:15 PM   #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.
tonderai76 is offline   Reply With Quote
Old 04-20-2004, 11:30 PM   #2
& the hat of GPL slaying
 
Thantos's Avatar
 
Join Date: Sep 2001
Posts: 5,732
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.
Thantos is offline   Reply With Quote
Old 04-20-2004, 11:41 PM   #3
jdm
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!
jdm is offline   Reply With Quote
Old 04-21-2004, 01:23 AM   #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;
}
Colie is offline   Reply With Quote
Old 04-21-2004, 09:52 AM   #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
major_small is offline   Reply With Quote
Old 04-21-2004, 10:21 AM   #6
Registered User
 
Codeplug's Avatar
 
Join Date: Mar 2003
Posts: 3,903
Welcome to the boards Colie.

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

gg
Codeplug is offline   Reply With Quote
Old 04-21-2004, 11:34 AM   #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
InvariantLoop is offline   Reply With Quote
Old 04-21-2004, 11:55 AM   #8
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>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.
Prelude is offline   Reply With Quote
Old 04-21-2004, 12:02 PM   #9
Registered User
 
Join Date: Mar 2004
Posts: 494
hmm teach yourself in 21 days
InvariantLoop is offline   Reply With Quote
Old 04-21-2004, 12:07 PM   #10
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>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.
Prelude is offline   Reply With Quote
Old 04-21-2004, 12:13 PM   #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.
InvariantLoop is offline   Reply With Quote
Old 04-21-2004, 12:49 PM   #12
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,664
>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.
Prelude is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive function Fork C Programming 3 10-26-2006 11:27 AM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
Calling a Thread with a Function Pointer. ScrollMaster Windows Programming 6 06-10-2006 08:56 AM
c++ linking problem for x11 kron Linux Programming 1 11-19-2004 10:18 AM
Contest Results - May 27, 2002 ygfperson A Brief History of Cprogramming.com 18 06-18-2002 01:27 PM


All times are GMT -6. The time now is 08:04 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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