473,405 Members | 2,294 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,405 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 2141
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.