need help to build a recursive function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adato
    New Member
    • Mar 2008
    • 5

    need help to build a recursive function

    hello.

    I have a problem to build a recursive function
    The function gets an array of prices, an array of weight and and spesific Price.
    the output's function is the minimum weighet of the subset and the summry of the subset values is equall or bigger then the specific Price.
    help me to build the function in a recursive way. (C)

    thank you
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Any recursion can be replaced by a loop.

    Therefore, get your code working using a loop.

    Then write a function that returns when the loop conditon is met. Otherwise, perform the loop instructions, adjuct the cycle counter and call the function again.

    Comment

    • adato
      New Member
      • Mar 2008
      • 5

      #3
      I have already done that

      I cant change the loop
      I dont know what is my stopping terms

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        If you have this loop:

        [code=c]
        for(int i=0; i <10; ++1)
        {
        printf("Hello\n ");
        }
        [/code]

        Then you can write this recursive function:
        [code=c]
        void MyFunction(int arg)
        {
        if (arg <10)
        {
        printf("Hello\n ");
        MyFunction(++ar g);
        }
        return;
        }
        main()
        {
        MyFunction(0);
        }
        [/code]

        Do you see how the loop appears inthe recursive function?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by weaknessforcats
          If you have this loop:

          [code=c]
          for(int i=0; i <10; ++1)
          {
          printf("Hello\n ");
          }
          [/code]

          Then you can write this recursive function:
          [code=c]
          void MyFunction(int arg)
          {
          if (arg <10)
          {
          printf("Hello\n ");
          MyFunction(++ar g);
          }
          return;
          }
          main()
          {
          MyFunction(0);
          }
          [/code]

          Do you see how the loop appears inthe recursive function?
          Yep, but you are showing a 'primitive recursive' function here; even a simpler
          'tail recursive' function. If you want to try it with a 'total recursive' function you
          at least need an explicit stack so a recursive solution is as good and even more
          efficient as an iterative solution. Try this one (the Ackermann function):

          [code=c]
          int A(int m, int n) {
          if (!m) return n+1;
          if (!n) return A(m-1, n);
          return A(m-1, A(m, n-1));
          }
          [/code]

          kind regards,

          Jos

          Comment

          Working...