Thread: Counting up(and down) using recursive function. Please help.

  1. #1
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Unhappy Counting up(and down) using recursive function. Please help.

    Dear Cprogramming.com administrators and members,


    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

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    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.

  3. #3
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Thumbs up 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

  4. #4
    Banned
    Join Date
    Nov 2007
    Posts
    678
    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.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    290
    Quote Originally Posted by MarkSquall View Post
    ... 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.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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.

  7. #7
    Registered User MarkSquall's Avatar
    Join Date
    Aug 2007
    Posts
    27

    Talking 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.
    "Listen to advice and accept instruction, and in the end you will be wise." -Proverbs 19:20

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  2. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  3. recursive function
    By tonderai76 in forum C++ Programming
    Replies: 11
    Last Post: 04-21-2004, 12:49 PM
  4. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM