CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Posts
    156

    how to get the output pattern of this function..

    Code:
    int what(int n,int  m)
    {
       if(!n && !m) return 0;
       if (n>m) return n + what (n-1,m);
       return what (n,m-1)-m;
    }
    (1,1) = 0
    (1,0) = 1
    (2,2)= 1

    i cant see a pattern
    Last edited by transgalactic; March 22nd, 2009 at 01:49 AM.

  2. #2
    Join Date
    Sep 2007
    Posts
    49

    Re: how to get the output pattern of this function..

    Your (2, 2) input is incorrect, it should be 0.

    Also, what's the context of this problem? If it's for homework I can give hints, but not the answer.

  3. #3
    Join Date
    Jan 2009
    Posts
    156

    Re: how to get the output pattern of this function..

    its questions from a test
    give me a system for picking output
    so i could get a formula out of it

  4. #4
    Join Date
    Sep 2007
    Posts
    49

    Re: how to get the output pattern of this function..

    If you want to just generate a table of inputs and outputs, why not just not write a test program raelly fast and display the results while looping through n = 0 to 10 and m = 0 to 10. If you look over that list you should be able to figure out the formula relatively easily.

    A few other things you can think about. This is a recursive function, and it's only non-recursive out put is 0, and only when both m and n are 0. If you look through the logic you should quickly be able to see that whenever n and m are equal, the output of the function is going to be 0. That's kind of a hint.

  5. #5
    Join Date
    Jan 2009
    Location
    Salt Lake City, Utah
    Posts
    82

    Re: how to get the output pattern of this function..

    Well, it looks like it returns the sum of all integers from 1 to n minus the sum of all integers from 1 to m.
    Also, it will infinitely loop if either n or m are negative
    Intel Core Duo Macbook w/ Mac OS 10.5.6
    gcc 4.2.1 (i386-apple-darwin9.1.0) and Xcode 3.1.1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured