recursive function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HighlightReel44
    New Member
    • Apr 2010
    • 11

    recursive function

    I need to write a recursive function to compute the sum of the first n terms of the following series

    1/3 - 2/5 + 3/7 - 4/9 + 5/11 - 6/13.....

    Thanks ina dvance for the help
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Hmmm....If you arranged to have the numerators of each fraction in one array and the denominators of each in a second array you could access each element in each array (each having the same index [] value) and divide one into the other.
    To decide on whether to add or subtract make the values with even indexes[] positive + and those with odd indexes [] negative -. the running sum+= could take care of calculating the series sum. Post some code to show us how you are progressing.

    Comment

    • HighlightReel44
      New Member
      • Apr 2010
      • 11

      #3
      Originally posted by whodgson
      Hmmm....If you arranged to have the numerators of each fraction in one array and the denominators of each in a second array you could access each element in each array (each having the same index [] value) and divide one into the other.
      To decide on whether to add or subtract make the values with even indexes[] positive + and those with odd indexes [] negative -. the running sum+= could take care of calculating the series sum. Post some code to show us how you are progressing.
      Any chance you can give me an example source code so i have something to follow along with

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        That array method is too confusing and unnecessary!

        so numerator = i
        and denominator = 2i+1?

        Have a parity flag.
        Code:
        loop on i -> n 
          if flag = positive
            add i/(2i+1)
            set flag = negative
          else // flag = negative
            subtract i/(2i+1)
            set flag = positive
        end loop
        Could we see how your source code differs so that we can help merge the algorithm in?

        Comment

        • HighlightReel44
          New Member
          • Apr 2010
          • 11

          #5
          Originally posted by jkmyoung
          That array method is too confusing and unnecessary!

          so numerator = i
          and denominator = 2i+1?

          Have a parity flag.
          Code:
          loop on i -> n 
            if flag = positive
              add i/(2i+1)
              set flag = negative
            else // flag = negative
              subtract i/(2i+1)
              set flag = positive
          end loop
          Could we see how your source code differs so that we can help merge the algorithm in?
          #include <iostream>
          using namespace std;

          int main()
          {
          double sum = 0;
          for (int i = 1; i <= 97; i += 2)
          sum += 1.0 * i / (i + 2);

          cout << "sum is " << sum;

          return 0;
          }


          This waht is tarted doin but i know its not right

          Comment

          • HighlightReel44
            New Member
            • Apr 2010
            • 11

            #6
            Originally posted by jkmyoung
            That array method is too confusing and unnecessary!

            so numerator = i
            and denominator = 2i+1?

            Have a parity flag.
            Code:
            loop on i -> n 
              if flag = positive
                add i/(2i+1)
                set flag = negative
              else // flag = negative
                subtract i/(2i+1)
                set flag = positive
            end loop
            Could we see how your source code differs so that we can help merge the algorithm in?
            I also formulated soemthing like this but this is only for a series with all positive values how do i change it to include the negatives


            #include <iostream>
            using namespace std;
            double seriesFunc(int n){
            if (n == 0) return 0;
            else if (n == 1) return 1/3.0;
            else return (n / (2 * n + 1)) + seriesFunc(n-1);}
            int main()
            { cout << seriesFunc(6) << endl;
            cout << seriesFunc(2) << endl;
            return 0;}

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              Try plugging in values:
              sum += 1.0 * i / (i + 2);
              > i = 1
              sum += 1.0 * 1 / (1 + 2);
              sum += 1 / 3;
              > i = 2
              sum += 1.0 * 2 / (2 + 2);
              sum += 2 / 4
              doesn't seem like what you want.
              ====
              Recursion:
              Notice when i is odd, you add. When i is even, you subtract.
              i%2 == 0 -> i is even
              So:
              Code:
              else if i is even  
                 return  - i/(2i+1) plus sum of the rest
              else 
                 return  +i/(2i+1) plus sum of the rest
              Last edited by jkmyoung; Apr 13 '10, 10:56 PM. Reason: small change

              Comment

              • HighlightReel44
                New Member
                • Apr 2010
                • 11

                #8
                Originally posted by jkmyoung
                Try plugging in values:
                sum += 1.0 * i / (i + 2);
                > i = 1
                sum += 1.0 * 1 / (1 + 2);
                sum += 1 / 3;
                > i = 2
                sum += 1.0 * 2 / (2 + 2);
                sum += 2 / 4
                doesn't seem like what you want.
                ====
                Recursion:
                Notice when i is odd, you add. When i is even, you subtract.
                i%2 == 0 -> i is even
                So:
                Code:
                else if i is even  
                   return  - i/(2i+1) plus sum of the rest
                else 
                   return  +i/(2i+1) plus sum of the rest
                Thanks a lot this was very much appreciated

                Comment

                Working...