Question

In: Computer Science

Pi Calculation implementation. If you watch the Discovery or Sci-Fi channel you will find dozens of...

Pi Calculation implementation.

  • If you watch the Discovery or Sci-Fi channel you will find dozens of alien conspiracy shows that reference Pi as something so advanced that it must be alien.  The number πis a mathematical constant, the ratio of a circle's circumference to its diameter, commonly approximated as 3.14159. So you could graph the value or calculate an approximate value using the series given below:

Pi = 4 * (1/1 – 1/3 + 1/5 – 1/7 + 1/9 … (alternating + -) (1/n))

First, Pi should be a double to allow many decimals. Notice that the numerator is always 1 or -1. If you started the numerator at -1, then multiply each term by -1. So if started the numerator at -1 and multiplied it by -1, the first numerator will be 1, then the next the numerator will be -1, alternating + and -.  

Then notice that the denominator goes from 1,3,5,7,9 etc.  So this is counting by 2s starting at one. For loops are good when you know how many times it will go through the loop. So a for loop might be something like:

for (long denom=1; denom <n; denom=denom+2)
where denom(inator) is the term that changes by 2 starting at 1 (not zero). We use a long to allow very large numbers

  • Remember, not every for loop starts at one, and not every for loop ends in i++!
  • Also remember that an int divided by int (or long/long) is an int, so you will have to convert the right hand side like double(1/1 – 1/3 + 1/5)
  • Note that 4 is multiplied outside the loop (after we have our series determined)

We are using longs, so we can have very long numbers. Likewise, PI should be a double (not a float) to have a very large decimal accuracy

Write a c++ program to calculate the approximate value of pi using this series.  The program takes an input denom that determines the number of values we are going to use in this series.  Then output the approximation of the value of pi. The more values in the series, the more accurate the data.  Note 5 terms isn’t nearly enough to give you an accurate estimation of PI. You will try it with numbers read in from a file. to see the accuracy increase. Use a while loop to read in number of values from a file. Then inside that loop, use a for loop for the calculation.

Solutions

Expert Solution

Thanks for the question, here is the simple program in PI in C++

==================================================================

// Calculate the value of PI

#include<iostream>

using namespace std;

int main(){

               

                long nTerms;

                cout<<"Enter the number of values to be used: "; cin>>nTerms;

               

                double pi=0.0;

                long den=1;

                for(long i=1;i<=nTerms;i++){

                                den = 2*i-1;

                                pi += (i%2==1)? 1.0/den:-1.0/den;

                }

               

                pi *=4;

               

                cout<<"Approximate value of PI using "<<nTerms<<" terms is : "<<pi<<endl;

}

==================================================================


Related Solutions

Find the channel that caused a channelopathy disease. Explain the structure of the channel you find...
Find the channel that caused a channelopathy disease. Explain the structure of the channel you find and how it works?
FYIPI (Find yourself in PI) In this assignment you will find a numeric string (if it...
FYIPI (Find yourself in PI) In this assignment you will find a numeric string (if it exists) within a file containing the first 1 million characters of the decimal expansion of PI. The numeric string in question is a 6 character string representing your birth date. E.g., if your birth date is January 1, 1984, then the string is 010184. The file containing the first 1 million characters of the decimal expansion of PI is named pidigits.txt and is available...
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it...
JAVA: (Find yourself in PI) In this assignment you will find a numeric string (if it exists) within a file containing the first 1 million characters of the decimal expansion of PI. The numeric string in question is a 6 character string representing your birth date. E.g., if your birth date is January 1, 1984, then the string is 010184. The file containing the first 1 million characters of the decimal expansion of PI is named pidigits.txt and is available...
Watch The Responsive Brain (you will have to scroll down to find the video). It's about...
Watch The Responsive Brain (you will have to scroll down to find the video). It's about 30 minutes, but you are only required to watch the first 9:30 (you can end at Excerpts from Grief and Peril in Infancy) and then skip to 21:57 (the African plain scene) and watch to the end. Please review the study on the impact of human touch on premature infants in intensive care. What was the hypothesis? What was the IV? What was the...
Watch a scene from Mozart’s Marriage of Figaro (You may find this selection by searching the...
Watch a scene from Mozart’s Marriage of Figaro (You may find this selection by searching the Internet.). In 200 – 250 words, briefly describe what is happening in the scene and your reaction to it. What action is happening? Is the scene funny? Who are the characters? Is this scene entertaining or interesting?
Can you find me TWO journal articles that outline the fundamental issues with the implementation of...
Can you find me TWO journal articles that outline the fundamental issues with the implementation of “public-private” partnerships in addressing difficulties in the interplay between environmental policy and technological innovation.
You are to find a media presentation (movie, documentary, series) to watch. After watching write up...
You are to find a media presentation (movie, documentary, series) to watch. After watching write up a brief summary of the piece and your reaction. (CONSPIRACY THEORIES-IDS)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT