Recursion help....PLEASE!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 67
Reputation: still_learning is an unknown quantity at this point 
Solved Threads: 0
still_learning still_learning is offline Offline
Junior Poster in Training

Recursion help....PLEASE!!!

 
0
  #1
Oct 23rd, 2007
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 <iostream>

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 <<endl;
n = print(--n);
}

int printup(int m)
{
if (m == 0)
return 0;

for (int i = 0; i < 5; i++)
if(i>=m)
cout << "* ";

else
cout << " ";

cout << endl;
m = printup(--m);


}
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 67
Reputation: still_learning is an unknown quantity at this point 
Solved Threads: 0
still_learning still_learning is offline Offline
Junior Poster in Training

Re: Recursion help....PLEASE!!!

 
0
  #2
Oct 23rd, 2007
Anyone? Please give some input if you have any knowledge on this subject. We are pressed for time. Thanks
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Recursion help....PLEASE!!!

 
0
  #3
Oct 23rd, 2007
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...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 67
Reputation: still_learning is an unknown quantity at this point 
Solved Threads: 0
still_learning still_learning is offline Offline
Junior Poster in Training

Re: Recursion help....PLEASE!!!

 
0
  #4
Oct 23rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Recursion help....PLEASE!!!

 
0
  #5
Oct 23rd, 2007
Yep. You're at a disadvantage alright...

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.

  1. #include <stdio.h>
  2.  
  3. void recursiveFunction(int val)
  4. {
  5. // The working area of the function
  6. printf("called value is %d \n", val);
  7. val--; // decrement val
  8.  
  9. // The test of the end-value
  10. // In this simple example it's simply waiting until val becomes 0
  11. if (val > 0)
  12. {
  13. recursiveFunction(val); // >0 call again
  14. }
  15. else
  16. {
  17. return; // <= 0, done
  18. }
  19.  
  20. // Returns will come here and val will be restored to
  21. // it's previous value
  22. // Do the next 'work'
  23. printf("return value is %d \n", val);
  24.  
  25. return;
  26. }
  27.  
  28. int main()
  29. {
  30. recursiveFunction(5); // start the function off
  31.  
  32. return 0;
  33. }

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...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 67
Reputation: still_learning is an unknown quantity at this point 
Solved Threads: 0
still_learning still_learning is offline Offline
Junior Poster in Training

Re: Recursion help....PLEASE!!!

 
0
  #6
Oct 23rd, 2007
Thanks for the help, I am trying to put something together right now.
Last edited by still_learning; Oct 23rd, 2007 at 6:22 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 42
Reputation: Pikachumanson is an unknown quantity at this point 
Solved Threads: 1
Pikachumanson's Avatar
Pikachumanson Pikachumanson is offline Offline
Light Poster

Re: Recursion help....PLEASE!!!

 
0
  #7
Jul 16th, 2008
I know this a long dead subject but... if anyone else has this problem like I did!
here is the solution
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void spaces(int x) //I'm thinking, everybody else is going to do a diamond full of stars. So why not
  5. //put spaces in mine and show the outline of the shape
  6. {
  7. for (int c = 1; c <= x; c++)
  8. cout << " ";//the spaces
  9. }
  10.  
  11. void stars(int x)//the stars
  12. {
  13. for (int c = 1; c <= x; c++)
  14.  
  15. {
  16. cout << "*";
  17. }
  18.  
  19. }
  20.  
  21. void row(int r, int rows) //print out each row
  22. {
  23. spaces (rows - r);
  24. stars (1);//outline of diamond
  25. stars (2 *(r - 1));//fills up inside of daimond.
  26.  
  27.  
  28. stars(1);
  29.  
  30. cout << endl;
  31. }
  32.  
  33. void top_row(int rows)
  34. {
  35. spaces (rows);
  36. stars (1);
  37. cout << endl;
  38. }
  39.  
  40.  
  41. int getDiff(int X, int Y) { // Tail Implementation
  42. X++; // Inc X
  43. if (X == Y)
  44. return 1;
  45.  
  46. return (1+getDiff(X, Y));
  47. }
  48.  
  49.  
  50. void getDiff2(int X, int Y, int Diff = 0) //subtracting x from y
  51. {
  52. X++;
  53. Diff++;
  54.  
  55. if (X != Y)
  56. getDiff2(X, Y, Diff);
  57. else
  58. cout << "Diff Was: " << Diff << endl;
  59.  
  60. return;
  61. }
  62.  
  63. int main(void) {
  64.  
  65.  
  66.  
  67. int r, rows;
  68. cout << "Size of diamond? ";//input the number
  69. cin >> rows;
  70. cout << "Difference Between 1 & 7 Is: " << getDiff(1, 7) << endl;//getting the difference between 1 and 7
  71.  
  72. top_row(rows);
  73. for (r = 1; r <= rows; r ++) row(r, rows);
  74. for (r = rows - 1; r >= 1; r --) row (r, rows);//This is where the equation for recursions takes place
  75. top_row(rows);
  76.  
  77. return 0; //end program
  78. }
Helps this help others out
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC