• 03-23-2008
MarkSquall

Hello everyone, a pleasant day to you all. I hope that everyone's doing great upon reading this forum. Well, I'm sort of experimenting with recursive functions and I feel great when I learned how to program them especially when using it on factorials, fibonacci sequence, etc. I have this sample program, here's the simple code:

Code:

```#include <iostream> class Counting {     public:         int count (int x, int y) { //this is a countdown             int ctr=0;             if (x==y)                 return 1;             else {                 ctr = y+count (x-1,y);                 return ctr;                      }         }//end of count() }; int main() {         int in, ctr, choice;         Counting countmain;         std::cout << "Please select from the menu: " << std::endl << std::endl;         std::cout << "[1] count up " << std::endl;         std::cout << "[2] count down " << std::endl;         std::cout << "\nPlease enter your choice: ";         std::cin>> choice;         if (choice==1){             std::cout<<" count from 1 up to ";             std::cin>> in;             for(ctr=1; ctr<=in; ctr++)                           std::cout << countmain.count(in,1) << std::endl;         }         return 0; }```

Well, this program will allow user to choose whether he/she wants to count up or count down so to speak. If the user choose 1, then the program will count from 1 up to n, where n is the number he/she input. For example I choose 1 then enter 10, so that means the program will count from 1 to 10, but unfortunately, the recursive function printed number 10, but ten times, not 1, 2, 3,...10 as I want. I just want to ask everyone's help on what changes should I made to my code make the member function count() to count down numbers. I know this can be relatively easy using loops, but somehow I'm just trying it on recursive functions. I hope someone could help me on this one.

Thank you in advance and God bless everyone.

Respectfully yours,

MarkSquall
• 03-23-2008
arpsmack
You could pass a third parameter to count telling it how much to increment by (negative if you want it to count down).
Code:

`int count(int start, int increment, int end);`
Of course, you'd have to handle the 0 increment case specially.
• 03-24-2008
MarkSquall
Thanks to you...
Dear Mr. arpsmack,

I want to say thank you for the idea you've shared, I'll try to work on your suggestions, Although there is one site I visited out of "nowhere," the site have only one parameter then it truly count down and up (using recursive function), but I will try to use your idea of three parameters too, and if I (successfully) done it, I'll be glad to post it here again to those who might find it useful some later time.

Thank you and God bless everyone.

Respectfully yours,

MarkSquall
• 03-25-2008
manav
you should be doing this!

Code:

```#include <iostream> class Counting {     public:         int count (int start, int stop) { //this is a countdown                 if (start<=stop) {                         std::cout << start << " ";                         count(start+1, stop);                 }         }//end of count() };```
in your code you are getting the result of last call.
• 03-25-2008
arpsmack
Quote:

Originally Posted by MarkSquall
... the site have only one parameter then it truly count down and up (using recursive function)...

I'm not sure how you would do this with only one parameter, unless you just assume its always going to stop at a certain number.

I suggested adding a third parameter because not only does it easily allow to you count both up and down, but it's a little more flexible (it allows you to count by 2's, 3's, etc...).

For example:
Code:

```count(0, 2, 10); ... would print: 0 2 4 6 8 10```
On the downside:
Code:

```count(0, 0, 10); // is an infinite loop! count(0, -1, 1); // will recurse a lot if you're not careful, and will probably cause a stack overflow```
You would have to write count carefully to account for those special cases.
• 03-25-2008
brewbuck
It's good to see somebody who likes recursion. Just keep in mind that C is not exactly the world's most recursion-friendly language. If you really enjoy programming in this manner, there are languages specifically designed for it. Just a heads up.
• 06-06-2008
MarkSquall
Thanks you. P.S. any Java forum you know?
Dear Mr. brewbuck, Mr. arpsmack, Mr. manav:

Thanks guys again for the information, and I want to thank brewbuck for such a good compliment. What makes really fun here is solving the problem I have in C/C++ programming together with everyone's help. I am really glad I come across to this site.

I have more questions to post here soon, so I hope everyone will be patient enough to teach me C and C++. :) God bless.

Respectfully Yours,

MarkSquall

P.S.
But how about Java? Is there a nice forum like this one about Java programming? Hope someone could share it too.