Question: Help here! Rewrite problem #1 using a static input (written into the code) of 10,000,000 iterations, the output should be written to a file named:
Help here!
Rewrite problem #1 using a static input (written into the code) of 10,000,000 iterations, the output should be written to a file named: problem1fileoutput.txt, and do not use the same type of loops as in problem #1. The file should be included as a separate attachment when uploading your work, not within the lab assignment document.
#include
#include
#define PI 3.14159265358979323846
using namespace std;
// method to return a term of Leibniz formula given the denominator and sign
double Leibniz(int denominator, int sign)
{
return sign*(1/(double)denominator);
}
int main()
{
int i, number_of_iteration;
// accepting user input
while(true)
{
cout << "Enter a number between 1000 and 100,000 to calculate pi: ";
cin >> number_of_iteration;
if(number_of_iteration >= 1000 && number_of_iteration <= 100000)
break;
cout << "Please enter number within correct range ";
}
int sign = 1;
double pi_value = 0.0;
// iterate for the given value
for(i=1; i<= number_of_iteration; i+=2)
{
// for each iteration, get leibniz value and add it
pi_value += Leibniz(i, sign);
// change sign after each term
if(sign == 1)
sign = -1;
else
sign = 1;
}
// display output
pi_value *= 4;
cout << " Number of iterations: \t\t" << number_of_iteration;
cout << " Result of Leibniz fr: \t\t" << pi_value;
cout << " Difference between both: \t" << fabs(pi_value - PI);
cout << " Error percentage: \t\t" << (fabs(pi_value - PI)/PI * 100);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
