what this function does..

This is a discussion on what this function does.. within the C Programming forums, part of the General Programming Boards category; Code: int what(int n,int m) { if(!n && !m) return 0; if (n>m) return n + what (n-1,m); return what ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    what this function does..

    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;
    }
    if i go n=10 m=5
    for the first if i get
    return 10+9+8+7+6 +what(5,5) which is
    return 10+9+8+7+6 +what(5,4)-5
    return 10+9+8+7+6 +5+what(4,4)-5


    what is the law??

  2. #2
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant see the pattern in the output

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    what will be the output of

    what(5,0)

    what(5,1)

    what(5,2)

    what(5,3)

    ...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    that takes too much time
    is there any simpler way?

  5. #5
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    use iteration instead of recursion if you want speed, as far as simplicity goes, its simple enough

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what??

  7. #7
    apprentiCe
    Join Date
    Oct 2008
    Location
    Hyderabad,India
    Posts
    136
    use for or a while loop instead of call the function over and over again

  8. #8
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    What transgalatic is doing, is saying that it takes too much time to calculate the output in an exam, where everything is time-critical.

    Try it for the cases of (1,1), (1, 2), (2, 1), (2, 2), (3, 1), (1, 3), (3, 2), (2, 3), (3, 3). Not very many cases, but it should give you a general idea of what the function does.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    (1,1) = 0
    (1,0) = 1
    (2,2)= 1

    i cant see a pattern

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by transgalactic2 View Post
    (1,1) = 0
    (1,0) = 1
    (2,2)= 1

    i cant see a pattern
    That's probably true. I would wager that no one would be able to find a pattern just from those three values. If you can't find a pattern from the first three values, keep going.

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont have a sistematic way..

    i am shooting in the dark
    i need a sistematic way for picking input

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Even if you don't know any numbers other than 0, 1, and 2, there are still six more input pairs that just use those numbers. (That is to say, you're going to need to know what happens everywhere, so look everywhere.) If you ever figure out any numbers other than 0, 1, and 2, then you can add that to your repertoire and make more pairs.

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i dont have a sistematic way..

    i am shooting in the dark
    i need a sistematic way for picking input in order to get a formula

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm not sure why you don't recognize "pick all the small inputs" as a system.

    If you did look at the code at all, then you would have noticed that there are two different modes of moving -- decrease the first number if you're "below the diagonal" (i.e., if the first is bigger than the second), decreasing the second number if you're on or above the diagonal. So you should be able to see that you'll need to pick pairs in all three places (below the diagonal, on the diagonal, and above the diagonal).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  4. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 07:44 PM

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