Solved

Recursive Function to calculate pi (Using gregory series)

Posted on 2004-03-28
36
1,478 Views
Last Modified: 2010-04-15
I need to know how to write a recursive function to calculate pi using the gregory series...based on user input.  

For example I'm requesting the user to input the number of terms to calculate pi based on the Gregory series.  Please help!

Thank you  


0
Comment
Question by:GJMill79
  • 18
  • 18
36 Comments
 
LVL 17

Expert Comment

by:mokule
Comment Utility
Is it homework?
You can find some theory there
http://mathworld.wolfram.com/GregorySeries.html
0
 

Author Comment

by:GJMill79
Comment Utility
Yes it is...thank you for the comment.

I know how to calculate pi with the gregory series in C without a function but I can't figure out how to utilize a recursive function to calculate pi using the gregory series.
0
 

Author Comment

by:GJMill79
Comment Utility
Although this doesn't seem to work (I recieve a seg fault)...I thought I would post my guess

double piby4(int terms)
{

        if (terms==1)
        return(piby4(1.0/terms+terms-1));
       
        else if (terms %2==0)
           return(piby4(-1.0/terms+terms-1));
           
        else if (terms %2)
           return(piby4(+1.0/terms+terms-1));
       
        else
           return (0);
       
}

down in int main

double pi, pidiv4;
int terms;

printf("How many terms do you want to use to calculate pi?  ");
fgets(buffer, sizeof(buffer), stdin);      
sscanf(buffer, "%d", &terms);
                  
                          pidiv4=piby4(terms);
                          pi=pidiv4*=4;
              
printf("The term you entered is %d\n", terms);
printf("The pi value is %0.6f\n", pi);
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
The error is You never stops calling piby4
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
Oops not only
What do You mean here
piby4(1.0/terms+terms-1);
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
In declaration piby4 has integer argument and in call You try to pass float
0
 

Author Comment

by:GJMill79
Comment Utility
I'm thinking that since the Gregory series is pi/4=1/1-1/3+1/5-1/7...
Bases on the user input for # of terms That I'm returning to int main() piby4(1.0/terms+terms-1) ...until C decrements terms to 1....but I'm not exactly sure how a recursive function works.... I need help in putting it together basically.
0
 

Author Comment

by:GJMill79
Comment Utility
If I'm trying to pass an int how do I return a float?
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
1.
First tell me what do You mean by terms
1,2,3,4, etc
or
1,3,5, etc

2.
If Your intension was to stop here You shouldn't call piby4
        if (terms==1)
        return(piby4(1.0/terms+terms-1));

3.What argument You want to pass to piby4
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
Passing argument has nothing to do with returning value.

for example
double fun(int k)
{
if(k==1)
  return 13.25;
else
  return 54.321;
}
You passing integer and returning double.
0
 

Author Comment

by:GJMill79
Comment Utility
By terms I mean 1/1 equals 1 term, 1/1-1/3 equals two terms, 1/1 - 1/3 + 1/5 equals three terms etc...

0
 

Author Comment

by:GJMill79
Comment Utility
"In declaration piby4 has integer argument and in call You try to pass float"...not sure what you mean by this

0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
double piby4(int terms)
{
        if (terms==1)
        return(piby4(1.0/terms+terms-1));
       
        else if (terms %2==0)
           return(piby4(-1.0/terms+terms-1));
           
        else if (terms %2)
           return(piby4(+1.0/terms+terms-1));
       
        else
           return (0);
}
--------------------------------
Your argument in this call
piby4(1.0/terms+terms-1)
is
1.0/terms+terms-1
and it float
0
 

Author Comment

by:GJMill79
Comment Utility
Do I need to change double piby4 to (double terms) instead of (int terms)

Is the basic setup of my function correct?  
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
double piby4(int terms)     // it is correct
{
        if (terms==1)
        return(piby4(1.0/terms+terms-1));       // don'call piby4 here - only return 1.
       
        else if (terms %2==0)
           return(piby4(-1.0/terms+terms-1));   // should be piby4(terms-1) - something
           
        else if (terms %2)
           return(piby4(+1.0/terms+terms-1)); // should be piby4(terms-1) + something
       
        else                         // it is not necesarry
           return (0);             // it is not necesarry

}

If You guess what is something it is done and You understand.
0
 

Author Comment

by:GJMill79
Comment Utility
is it -1 and +1 ?
0
 

Author Comment

by:GJMill79
Comment Utility
so your're saying return(piby4(terms-1) - something)?---is it -1.0/terms
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
I'm sorry, no
Look at the basic formula
for example
piby4(5) should be equivalent to 1/1-1/3+1/5-1/7+1/9    right?
and
piby4(4) should be equivalent to 1/1-1/3+1/5-1/7    right?
so
piby(5) = piby(4) + what

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Expert Comment

by:mokule
Comment Utility
Quite near but we stated that term is 1,2,3,4 etc
0
 

Author Comment

by:GJMill79
Comment Utility
Right!

piby4(5)=piby4(4) + (1/term+2)?
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
No. Back to my last example for term 5 what shoul it be?
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
Please look at the formula at my first link. It should be clear then
0
 

Author Comment

by:GJMill79
Comment Utility
piby4(5)=piby4(4) + piby4(terms+1)
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
Wrong direction
piby4(5) should be equivalent to 1/1-1/3+1/5-1/7+1/9    right?
and
piby4(4) should be equivalent to 1/1-1/3+1/5-1/7    right?
so
piby4(5) =  piby4(4) +1/9      right?
0
 

Author Comment

by:GJMill79
Comment Utility
I understand that but why would I put  +1/9 in my function if the terms change based on user input?
0
 

Author Comment

by:GJMill79
Comment Utility
So your saying:
else if (terms %2)
       return(piby4(+1.0/terms+terms-1)); // should be piby4(terms-1) +something
     
       so return(piby4(terms-1)+1/9?  How does the function know what to divide?


           
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
No You should put some expression that evaluates to -1/7 for term 4, 1/9 for term 5,  -1/11 for term 6, 1/13 for term 7,
0
 

Author Comment

by:GJMill79
Comment Utility
how will that work if the user can input any amount of terms?
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
Do You look at the link I provided at the beginning.
Please write me the denominator of the first formula on the page
0
 

Author Comment

by:GJMill79
Comment Utility
2k-1...are you saying that I need to use this formula in my function?
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
I don't now what You mean by this formula.
You use this formula by recursion and this expression about we are talking so long is
1./(2*term-1).
Please we must quickly come to an end.
It's getting here very very late.
0
 

Author Comment

by:GJMill79
Comment Utility
I'm sorry but I'm so confused now...

I'm not sure what I'm suppose to return?

do I put the formula in int main or the function?
0
 
LVL 17

Accepted Solution

by:
mokule earned 500 total points
Comment Utility
double piby4(int terms)     // it is correct
{
        if (terms==1)
//        return(piby4(1.0/terms+terms-1));       // don'call piby4 here - only return 1.
          return 1.;
       
        else if (terms %2==0)
//           return(piby4(-1.0/terms+terms-1));   // should be piby4(terms-1) - something
             return(  piby4(terms-1) + 1./(2*terms-1)   );
        else if (terms %2)
//           return(piby4(+1.0/terms+terms-1)); // should be piby4(terms-1) + something
             return(  piby4(terms-1) - 1./(2*terms-1)   );
       
//        else                         // it is not necesarry
//           return (0);             // it is not necesarry

}
0
 

Author Comment

by:GJMill79
Comment Utility
Thank you very much for all your help...since time is running out for you, I'll try this...
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
the line
        else if (terms %2)
better change with
        else
Greetings from Poland
We can come to this tomorrow. If You don't understand something yet.
0
 

Author Comment

by:GJMill79
Comment Utility
Thank you Mokule that answers my question perfectly!  I'll look you up in the future if I have anymore programming questions.

Greetings from the U.S.!
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now