Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,094 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,612 people online right now. Registration is fast and FREE... Join Now!




pi and C ++ programming

 
Reply to this topicStart new topic

pi and C ++ programming, approximation of Pi

ppilar
27 Sep, 2007 - 08:08 PM
Post #1

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 12


My Contributions
I have been trying to complete an assignment that tells you the approximation of Pi based on the equation Pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 . . . The user will input the number of terms to approximate Pi. I am lost on three things, 1.) what is the right equation to get pi to evaluate and 2.) how do you get pi to add then subtract then add . . . . 3.) is using fibonnaci the best way to go below is what I have been able to figure out so far any help would be soooooo awesome. Thanks

CODE

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

    
main()
{
      double pi1;
      double pi2;
      double termspi;
      double currentpi;
      int counter;
      int steps;
      
      
      cout << fixed << showpoint << setprecision(9) << endl;
      
      pi1 = (4);
      pi2 = ();
      int three;
      
      cout << "Program will approximate Pi" << endl;                            
      
      
      
      cout << "Enter the number of terms to use: ";
      cin >> termspi;
      cout << endl;
      
      if (termspi == 1)
          currentpi = pi1;
      else if (termspi == 2)
               currentpi = pi2;
      else
      {
          counter = 3;
          three = 3;
          
          while (counter <= termspi)
          {
               currentpi = ((three + 2) /4);
               three = pi1;
               pi1 = pi2;
               pi1 = currentpi;
               counter++;
          }
      }
      
      cout << "The approximation of pi at position " << termspi << " is" << currentpi << endl;
      
          
      
      
    
      


cout << endl <<endl;
    cout << "Press [enter] to exit" << endl;
    cin.ignore();
    cin.get();
return 0;

      }


MOD EDIT: added [code] tags ~ jayman9
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Pi And C ++ Programming
27 Sep, 2007 - 08:24 PM
Post #2

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
just a couple of thoughts to start you off...

initialize the number that you're going to be adding to/subtracting from to 0. the start of your equation is pi=4/1+..., so starting it off with zero allows you to do the calculation without the if (termspi == 1) instruction.

use a for loop, not a while loop. the following form will increment 1,3,5,7,9,...:

CODE
long i;
for (i=1; i<(2*termspi-1); i+=2) {
    //  do calulations here
}


to get the addition/subtraction right, you're probably going to need to use a conditional. you should note that the terms that are added have 1,5,9,... in the denominator, while the terms that are subtracted have 3,7,11,... I'll just put this out there for you to think about: if the term is to be added, the function (denominator-1)%4 is equal to 0; otherwise, it is greater than zero.

please post code in blocks, and you might want to clarify your questions a little more.

hopefully that will give you a starting point for some of this.

-jjh

This post has been edited by jjhaag: 27 Sep, 2007 - 08:27 PM
User is offlineProfile CardPM
+Quote Post

ppilar
RE: Pi And C ++ Programming
27 Sep, 2007 - 08:39 PM
Post #3

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 12


My Contributions
I am really sorry, Since I am new to this, what do you mean post your code in blocks? And what does long i mean?




QUOTE(jjhaag @ 27 Sep, 2007 - 09:24 PM) *

just a couple of thoughts to start you off...

initialize the number that you're going to be adding to/subtracting from to 0. the start of your equation is pi=4/1+..., so starting it off with zero allows you to do the calculation without the if (termspi == 1) instruction.

use a for loop, not a while loop. the following form will increment 1,3,5,7,9,...:

CODE
long i;
for (i=1; i<(2*termspi-1); i+=2) {
    //  do calulations here
}


to get the addition/subtraction right, you're probably going to need to use a conditional. you should note that the terms that are added have 1,5,9,... in the denominator, while the terms that are subtracted have 3,7,11,... I'll just put this out there for you to think about: if the term is to be added, the function (denominator-1)%4 is equal to 0; otherwise, it is greater than zero.

please post code in blocks, and you might want to clarify your questions a little more.

hopefully that will give you a starting point for some of this.

-jjh


This post has been edited by ppilar: 27 Sep, 2007 - 08:47 PM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Pi And C ++ Programming
27 Sep, 2007 - 09:08 PM
Post #4

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
no prob. take a look at the grey text on the background of the text box that you reply in (it can be tough to see on some monitors, and it's intentionally inconspicuous). alternatively, just highlight your code, and click the # button above the text box to put it into a block. jayman9 edited your original post and added them for you, but you'll have to do it in the future.

long i means you are declaring a variable i of type long, similar to how you used double pi2. i could also have used just a normal int, but it's a weird habit i got into for reasons i won't go into. it can also be written as

long int i;

if you haven't encountered the long int datatype before, you may want to take a look at some introductory C/C++ tutorials.

-jjh
User is offlineProfile CardPM
+Quote Post

ppilar
RE: Pi And C ++ Programming
2 Oct, 2007 - 05:08 AM
Post #5

New D.I.C Head
*

Joined: 27 Sep, 2007
Posts: 12


My Contributions
I am still completley lost as to how you get C++ to calculate Pi. using the equation Pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 . . . . I figured out how to get the denominator to change but I am clueless. Please if anyone can help I really am stuck.
below is what I have done so far
CODE
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

    void printMessage(string);
    int returnNumber(string);

int main()
{
    char again;
    int termspi,
        steps,
        num;
    
    int countDenominator;
    float pi;    
    const double NUMERATOR = 4.0;
    
    cout << " Program will approximate Pi ";
    cout << endl << endl;    
        

    termspi = returnNumber ("Enter the number of terms to use: ");
    cout << endl << endl;
    
      
for (int num = 1; num <=(2*termspi-1); num += 2){
    if  ((num-1)%4 != 0)
    cout << 4.0 /-num << endl;
    else
    cout << 4.0/num << endl;
}

    steps = returnNumber("Display Pi after every how many steps?: ");


int count = 1;


while (steps < termspi) {
    steps += 2 * count;
    count += 1;
}
cout << "num = " << steps << endl;
cout << "count = " << count << endl;





do {
        cout << "Enter Y to run program again" << endl;
        cout << "(or any other key to exit)" << endl;
        cin >> again;
    } while ((again == 'Y') || (again == 'y'));
    return 0;
}

void printMessage(string msg)
   {
     cout << msg;
   }

int returnNumber(string msg)
   {
     int num;
     cout << msg;
     cin >> num;
     while (num < 1){
         cout << "Your input is not valid !!!" << endl;
         cout << "Please Re-enter a positive non zero number: ";
         cin >> num;}
         cout << endl;
     return num;
  
}


This post has been edited by ppilar: 2 Oct, 2007 - 05:10 AM
User is offlineProfile CardPM
+Quote Post

jjhaag
RE: Pi And C ++ Programming
2 Oct, 2007 - 11:20 AM
Post #6

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
you have all of the steps there. instead of displaying the value of the term using the cout statement, add it to the current value of pi, just like your equation says your supposed to.

-jjh
User is offlineProfile CardPM
+Quote Post

Plutonic
RE: Pi And C ++ Programming
3 Oct, 2007 - 05:11 PM
Post #7

New D.I.C Head
*

Joined: 15 Jul, 2007
Posts: 9


My Contributions
You could try something like this to get it going.

CODE

int main(int argc, char *argv[])
{
    long n;
    float pi;
    int pos;
    
    for (n=1; n <= 99999999; n++)
     {
        if ( (n % 2) == 0 ) {
           pos = -1;
           }
        else   {
            pos = 1;
            }
            
        pi = pi + (float) (4*pos)/(2*n-1);
        }


I've basically taken the pi formula and turned it into a sequence. What pos does is check whether to make the number in the sequence (represented by 'n') positive or negative.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 08:29PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month