473,382 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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
Apr 12 '10 #1
7 2140
whodgson
542 512MB
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.
Apr 12 '10 #2
@whodgson
Any chance you can give me an example source code so i have something to follow along with
Apr 12 '10 #3
jkmyoung
2,057 Expert 2GB
That array method is too confusing and unnecessary!

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

Have a parity flag.
Expand|Select|Wrap|Line Numbers
  1. loop on i -> n 
  2.   if flag = positive
  3.     add i/(2i+1)
  4.     set flag = negative
  5.   else // flag = negative
  6.     subtract i/(2i+1)
  7.     set flag = positive
  8. end loop
  9.  
Could we see how your source code differs so that we can help merge the algorithm in?
Apr 12 '10 #4
@jkmyoung
#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
Apr 13 '10 #5
@jkmyoung
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;}
Apr 13 '10 #6
jkmyoung
2,057 Expert 2GB
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:
Expand|Select|Wrap|Line Numbers
  1. else if i is even  
  2.    return  - i/(2i+1) plus sum of the rest
  3. else 
  4.    return  +i/(2i+1) plus sum of the rest
Apr 13 '10 #7
@jkmyoung
Thanks a lot this was very much appreciated
Apr 20 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
4
by: Nicolas Vigier | last post by:
Hello, I have in my python script a function that look like this : def my_function(arg1, arg2, opt1=0, opt2=1, opt3=42): if type(arg1) is ListType: for a in arg1: my_function(a, arg2,...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
9
by: Bill Borg | last post by:
Hello, I call a function recursively to find an item that exists *anywhere* down the chain. Let's say I find it five layers deep. Now I've got what I need and want to break out of that whole...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
41
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
10
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive...
4
by: ThEoNeAnDOnLy | last post by:
I recently had an issue with my recursive project in class. Here is the code. // Recursion.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
3
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.