Recursion help....PLEASE!!!
Hi there,
My son has been recently hospitalized after getting into an accident. Now I am trying to help him with some of his projects. Right now I am stuck on this recursion problem. We need to figure out how to form a diamond shape out of *'s using a recursive function. In my code here, I am using two recursive functions, because I cannot figure out how to use just one. Also, if you run this program, you will realize the top half of the diamond works, and the bottom half doesn't seem to work correctly. Please take a look at my code and REMEMBER, I am completely new to this. I don't even work with computers and I am trying to catch on quick to help my son keep up with his school work while he is hospitalized for the next couple of weeks. Your help is much appreciated, and please try not to laugh at my effort : ) Below is the diamond shape that I am trying to accomplish, and the code that I am using.
....*....
...* *...
..* * *..
.* * * *.
..* * *..
...* *...
....*....
The periods are just spaces.
#include
using namespace std;
int printup(int m);
int print(int n);
int main(void)
{
printup(4);
print(3);
return 0;
}
int print(int n)
{
if(n == 0)
return 0;
for (int i = 0; i < n; i++)
if(i<=n)
cout << "* ";
else
cout << " ";
cout <=m)
cout << "* ";
else
cout << " ";
cout << endl;
m = printup(--m);
}
still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
Anyone? Please give some input if you have any knowledge on this subject. We are pressed for time. Thanks
still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
For starters, you typed right over the instructions on using code tags to make the code readable, and obviously didn't read the post titled "Read Me: Read This Before Posting"...
The recursive function must:
when called,
1) output a specified number of *s and SPACEs.
2) test if the value is at a defined end value
2a) if not, call again with the next value
2b) if so, return
when returned
1) output a specified number of *s and SPACEs exactly as above
2) return
That should do it...
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Thanks for the reply. I am all new to this stuff as I stated. Being a 49 year old guy who has never touched a computer really puts me at a disadvantage. I understand kind of what you are trying to tell me what to do in your instructions. But I have no idea how to actually go about doing that. If you could show me some example I would be so grateful. And sorry for not making the code readable, I had no idea that I had to do anything to make it readable.
still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
Yep. You're at a disadvantage alright... :icon_wink:
What I can do for you is give you a sample of a recursive function you can look at. It's up to you to think through the changes for your program.
#include <stdio.h>
void recursiveFunction(int val)
{
// The working area of the function
printf("called value is %d \n", val);
val--; // decrement val
// The test of the end-value
// In this simple example it's simply waiting until val becomes 0
if (val > 0)
{
recursiveFunction(val); // >0 call again
}
else
{
return; // <= 0, done
}
// Returns will come here and val will be restored to
// it's previous value
// Do the next 'work'
printf("return value is %d \n", val);
return;
}
int main()
{
recursiveFunction(5); // start the function off
return 0;
}
Output:called value is 5
called value is 4
called value is 3
called value is 2
called value is 1
return value is 1
return value is 2
return value is 3
return value is 4
PS: Hope he'll be better soon. And I hope he's doing most of these projects... :icon_wink:
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Thanks for the help, I am trying to put something together right now.
still_learning
Junior Poster in Training
73 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0