Thread: recursion function for c where did i wrong

  1. #1
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54

    Question recursion function for c where did i wrong

    Code:
    int degree(int x,int n)
    {
    int m=0;
    if(m<n)  degree(x*x,m++)
    else
    return x;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what will happen if you call this function with parameters 2 and 1? Could you imagine?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54

    Question no

    can you write me a functions for degree exp:2*2*2=8 or 2 on third

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    As vart was saying...
    Quote Originally Posted by vart
    what will happen if you call this function with parameters 2 and 1? Could you imagine?
    Unless you start passing in negative numbers to degree, the test portion will always be true, which means that the function will run until it uses all the memory on the stack and crashes the program.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Maybe
    if(m<n) return degree(x*x,m++)

    It's at this point you get to learn about
    degree(x*x,m++)
    degree(x*x,++m)
    degree(x*x,m+1)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Why make it recursive? Something like this should probably not be recursive.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Most recursive student exercises are best done as a loop, but still you have to practice these things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by crvenkapa
    no
    if you write some code, you should be able to play a role of the code interpretator with some simple input value. How you write it otherwise? Just using random guess?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54

    recursion

    i m a student and i have an exam for recursion functions very soon. can some one wrote me a recursion for degree ??
    pls help me
    if some one now this
    it can send me the code on
    my email
    <<snipped>>
    or you can tell me on
    my yahoo messenger::
    <<snipped>>
    Last edited by Salem; 01-13-2007 at 02:41 PM. Reason: Remove email addresses

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by crvenkapa
    i m a student and i have an exam for recursion functions very soon. can some one wrote me a recursion for degree ??
    pls help me
    if some one now this
    it can send me the code on
    my email
    <address removed>
    or you can tell me on
    my yahoo messenger::
    <address removed>
    The answer is no.

    Did you read the "homework" announcement? We don't do homework here.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    C'mon, you're close to the answer already.
    What else have you tried since your first post?
    Was it any better, what different answers did you get, did you learn anything?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54
    i tried everything i learned when i call the function again
    m is again zero and for that my code is does not work i
    dont now how to fix that..
    i m new in programming

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int m=0; - this is the first line in the function and it says to set the m to 0

    the recusive representetion of the degree:
    x^n = x* x^(n-1)

    f(x,n) = x * f(x,n-1)
    it is correct for n>1

    and f(x,0) = 1
    f(x,1) = x

    could you now fix your code to calculate f(x,n)?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54

    tnx

    i find my problem
    and i can fix it
    tnx very much

  15. #15
    Performer
    Join Date
    Jan 2007
    Location
    Macedonia
    Posts
    54
    the code for degree is
    Code:
    int degree(int x,int n)
    {
    if(n==0) return 1;
    if(n==1) return x;
    return degree(x,n-1)*x;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM