Question

Part II - Pi Calculation implementation. If you watch the Discovery or Sci-Fi channel you will...

Part II - 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.

  • Create an input file called lab04in.txt. Place the following numbers in the file. Read in long data types (instead of int)

12

123

1234

12345

123456

1234567

12345678

123456789

  • You should work with your lab partner on this, but each of you must write your owncode.
  • Implementation Detail 1: Remember that an integer divided by an integer is of type integer (or long/long is a long).  Make sure you use at least one decimal point in your division (like 1.0/9) or double(numerator)/denom
  • Implementation Detail 2:The default for the cout of a double is 5 places of accuracy.  If you want to increase that you would use setprecision.  Use setprecision (20)
  • http://www.cplusplus.com/reference/iomanip/setprecision/
  • Build the program. Fix any errors, then run it. Take a screenshotand place it below

Homework Answers

Answer #1

Code

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
long double calculatePi(long n);
int main()
{
   ifstream inFile;
   inFile.open("lab04in.txt");
   long n;
   cout<<fixed<<setprecision(10);
   while(inFile>>n)
   {
       cout<<"For "<<n<<" turms pi= "<<calculatePi(n)<<endl;
   }
}
long double calculatePi(long n)
{
   int count=1;
   long double pi=0;
   for (long denom=1; denom <n; denom++)
   {
       if(denom==1)
       {
           pi =4;   
}
else if(denom%2==0)
{
pi = pi - (4.0/(2.0*denom-1.0));
}
else
       {
           pi = pi+(4.0/(2.0*denom-1.0));
       }
       count++;
}
   return pi;
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159;...
#include<iostream> #include<iomanip> using namespace std; int main() { //variables int choice; float radius,base,height,area; const double PI=3.14159; //repeat until user wants to quits while(true) { //menu cout<<endl<<endl<<"Geometry Calculator"<<endl<<endl; cout<<"1. Calculate the area of a circle"<<endl; cout<<"2. Calculate the area of a triangle"<<endl; cout<<"3. Quit"<<endl<<endl; //prompt for choice cout<<"Enter your choice(1-3): "; cin>>choice; cout<<endl; //if choice is circle if(choice==1) { cout<<"What is the radius of the circle? "; cin>>radius; //calculating area area=PI*radius*radius; cout<<endl<<"The area of the circle is "<<fixed<<setprecision(3)<<area<<endl; } //if choice...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the...
Description: In this assignment, you need to implement a recursive descent parser in C++ for the following CFG: 1. exps --> exp | exp NEWLINE exps 2. exp --> term {addop term} 3. addop --> + | - 4. term --> factor {mulop factor} 5. mulop --> * | / 6. factor --> ( exp ) | INT The 1st production defines exps as an individual expression, or a sequence expressions separated by NEWLINE token. The 2nd production describes an...
In this lab, you complete a partially prewritten C++ program that uses an array. The program...
In this lab, you complete a partially prewritten C++ program that uses an array. The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help...
Provide a complete solution to the following problem using the C++ language in a SINGLE file...
Provide a complete solution to the following problem using the C++ language in a SINGLE file with a .cpp file extension. READ THE ENTIRE QUESTION (AND THE EXAMPLE) CAREFULLY BEFORE DEVELOPING YOUR SOLUTION! Design the code for a class called Pi, which will be used to encapsulate the value of pi stored as a string. (example ONLY) "3.141592654" | |_ _ _ _| whole number portion_| |_ _ _ _ _fractional portion (9 digits) The Pi class has the following...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from...
For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from a file. Specifically, you will implement the following function: /* * Reads one arithmetic "expression" at a time from a file stream, computes, then * returns the result. If there are additional expressions in the file, they are * read and computed by successive calls to “calculator”. * * “Expressions” are groups of operations (add, subtract, multiply, divide). Your * calculator will read and...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...