Thread: Have question on implementing a certain recursive function

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    225

    Have question on implementing a certain recursive function

    First of all, do NOT post complete or partial solutions, only help. Second of all, I am having trouble implementing the recursive function in 2 homework programs I'm doing.

    The first one sums multiples of 3, and the second one does combinations (number choose number) but I have no clue how to implement it. I have tried by myself, but both programs are a disaster.


    Code:
    #include <iostream>
    using namespace std;
    int sum3s(int sum) {
        if(sum == 1) {
               return 0;
               }
               else if (sum / 3 + 1 && 3 + 2 && 3 + 3 == 3) {
                    
                    return 1 + 3 + 2 + 3 + 3 + 3 * sum3s(3 * 3 * sum);
    }
    }
    int main ( ) {
        int number;
        cout << "Enter number: ";
        cin >> number;
        cout << "" << endl;
        cout << "The sum is " << sum3s(number);
        cout << ".";
    }
    Code:
    #include <iostream>
    using namespace std;
    int combinations (int n, int k) {
        if (n == 1) {
              return 1;
              }
              else {
                   return combinations(n + k + n + k + n + k);
    }
    }
    int main ( ) {
        int a;
        int b;
        cout << "Enter an Integer: ";
        cin >> a;
        cout << "Enter another Integer: ";
        cin >> b;
        cout << "Combinations( " << a "," << b ")" " = ";
        cout << combinations(a, b);
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Can you give input and output examples?
    E.g. for input X, you get output Y.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    225
    Here is an example:

    On the one where I sum multiples of 3, I put in 10 (or any number) and get 0. I should get 18.

    The other one just doesn't compile.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You failed to mention an example for the second one.
    Anyway, so the first one... what is your strategy for summing the multiples? As far as I see, your code is just mumbo jumo added together.
    It doesn't have to be recursive. First you need a working algorithm, even if it's iterative.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Sum of Multiples of 3 (Recursion) - C++ Forum

    ^ Same question phrased a little better (at least the first question..no idea on the combinations)

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    The conditional statement you created
    Code:
    else if (sum / 3 + 1 && 3 + 2 && 3 + 3 == 3)
    is equivalent to
    Code:
    else if ((sum / 3 + 1) != 0 && true && false)
    which will be evaluated always as
    Code:
    else if (false)

  7. #7
    Rat with a C++ compiler Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    203
    Maybe I'm misreading or I'm crazy but

    Code:
    int combinations (int n, int k) {
        if (n == 1) {
              return 1;
              }
              else {
                   return combinations(n + k + n + k + n + k);

    In your recursive statement, you are running combinations with one parameter, and I don't see a default parameter value. How does this compile?
    How to ask smart questions
    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesOverwritten);

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    107
    What does code in your sig do?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As far as I know, it hacks the windows task manager's code by overwriting the first instruction in the NtQuerySystemInformation function with an instruction to jump back to itself (thereby creating an infinite loop). At least, that's what I gather.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by Elysia View Post
    As far as I know, it hacks the windows task manager's code by overwriting the first instruction in the NtQuerySystemInformation function with an instruction to jump back to itself (thereby creating an infinite loop). At least, that's what I gather.
    NtQuerySystemInformation() is sometimes used to determine if a debugger is attached to your process (though there are other ways). So you might do something like this if you were trying to cause a program to stop right inside its "anti-debugger" code, so you could thereby attach a debugger and dump the stack to get a picture of where the copyright enforcement mechanism is located. So you can then hack around it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  2. recursive function question
    By cosmic_cow in forum C Programming
    Replies: 10
    Last Post: 08-13-2009, 03:08 PM
  3. transforming this loop into a recursive function question
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 01-22-2009, 05:11 AM
  4. recursive function question
    By hkl01 in forum C Programming
    Replies: 6
    Last Post: 12-26-2007, 02:52 PM
  5. question on recursive function
    By dionys in forum C Programming
    Replies: 5
    Last Post: 06-10-2004, 07:50 AM