Recursion

This is a discussion on Recursion within the C++ Programming forums, part of the General Programming Boards category; I am writing a program and I need help with a part of it. Need a recursive function that returns ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    1

    Angry Recursion

    I am writing a program and I need help with a part of it.

    Need a recursive function that returns the number of 1's in the binary representation of N. I am using the fact that this is equal to the number of 1's in the representation of N/2, plus 1, if N is odd.


    Can anyone assist me. tminott@firstindiana.com

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Answer these 2 questions and you should be able to write it yourself....

    1) What is the base case?
    2) How do I simplify towards the base case.

    The structure of a recursive function is normally like this...

    int func( int param)
    {
    if(BASECASE)
    {
    // this is where the real work of the function is...
    }
    else
    {
    // call func again with simpler terms nearer to the base case.
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  2. convert Recursion to linear can it be done
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 10-15-2008, 03:58 AM
  3. Recursion... why?
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 06-09-2008, 10:37 AM
  4. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM
  5. To Recur(sion) or to Iterate?That is the question
    By jasrajva in forum C Programming
    Replies: 4
    Last Post: 11-07-2001, 09:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21