All I need is a simple yes or no answer

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

All I need is a simple yes or no answer

 
0
  #1
Oct 23rd, 2007
Is this an example of a recursive function?? Somebody on another forum helped me put this together, as I am trying to make a diamond shape out of asterisks. But he did not know if this was a correct way of using recursion. Can somebody just tell me yes or no, is this a correct example of a recursive function?? Thanks

  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5.  
  6. int spaces = 5;
  7.  
  8. int main()
  9. {
  10.  
  11. for (int i = 1; i < 5; i = i + 1)
  12. {
  13. cout << setw(spaces);
  14. for (int j = 0 ; j < i; j++)
  15. cout << "* ";
  16. cout << endl;
  17. --spaces;
  18. }
  19. for (int i = 1; i < 4; i = i + 1)
  20. {
  21. cout << setw(spaces + 2);
  22. for (int j = 4 ; j > i; j--)
  23. cout << "* ";
  24. cout << endl;
  25. ++spaces;
  26. }
  27. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 43
Reputation: pacman326@gmail is an unknown quantity at this point 
Solved Threads: 0
pacman326@gmail pacman326@gmail is offline Offline
Light Poster

Re: All I need is a simple yes or no answer

 
0
  #2
Oct 23rd, 2007
I though recursion function wise is something like


void boo()
{
// blah blah blah
boo();
}

I may be wrong though.
Last edited by pacman326@gmail; Oct 23rd, 2007 at 6:44 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,871
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: All I need is a simple yes or no answer

 
0
  #3
Oct 23rd, 2007
Well, no. It's not.
Do you understand recursion? It's essentially another method of looping but rather than using while() or for(), for example, a function recursively calls itself:

  1. void count_down( int num ) {
  2. if ( num>=0 ) {
  3. std::cout<< num;
  4. cout_down( num-1 );
  5. // Calls the function again but this time the
  6. // parameter is one less than before. By this
  7. // you can iterate through all the values you
  8. // need to. Use control statements to determine
  9. // how to proceed.
  10. }
  11. }

That's not the most imaginative example of recursion but it should give you an idea.
Also. Using recursion on main() is normally considered not good.
Last edited by twomers; Oct 23rd, 2007 at 6:47 pm.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
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: All I need is a simple yes or no answer

 
0
  #4
Oct 23rd, 2007
Ok, I understand how recursion works and everything. And some kind guy gave me the following example to show me how a recursive function can decrement through numbers until it hits zero and then go back up and print those numbers. However, my problem is that I cannot figure out how to make it print the asterisks (*). For instance, I am trying to print a diamond shape, so I need line 1 to print 1 *, line 2 to print 2 *'s, line 3 to print 3 *'s...etc until it gets to 4 *'s and then go back down again to make a diamond shape. When I try to apply asterisks to this program, all I get of course is a line of asterisks like so:
*
*
*
*
* etc.....

Below is the code the guy gave me as an example. If you would please, look at that code and look at my code for printing a diamond, and tell me how I might go about putting the two together to print the asterisks. Thanks a lot to whoever saves the day for me.

  1. #include <stdio.h>
  2. void recursiveFunction(int val)
  3. {
  4. // The working area of the function
  5. printf("called value is %d \n", val);
  6. val--; // decrement val
  7. // The test of the end-value
  8. // In this simple example it's simply waiting until val becomes 0
  9. if (val > 0)
  10. {
  11. recursiveFunction(val); // >0 call again
  12. }
  13. else
  14. {
  15. return; // <= 0, done
  16. }
  17. // Returns will come here and val will be restored to
  18. // it's previous value
  19. // Do the next 'work'
  20. printf("return value is %d \n", val);
  21. return;
  22. }
  23. int main()
  24. {
  25. recursiveFunction(5); // start the function off
  26. return 0;
  27. }
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
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: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: All I need is a simple yes or no answer

 
0
  #5
Oct 23rd, 2007
Originally Posted by still_learning View Post
Is this an example of a recursive function?? Somebody on another forum helped me put this together, as I am trying to make a diamond shape out of asterisks. But he did not know if this was a correct way of using recursion. Can somebody just tell me yes or no, is this a correct example of a recursive function?? Thanks
No.
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  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC