Question

In: Computer Science

use for and while loop, also ifstream, ofstream, setprecision, and iomanip. Pi = 4 * (1/1...

use for and while loop, also ifstream, ofstream, setprecision, and iomanip.

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

Solutions

Expert Solution

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.


Related Solutions

Write a program that uses loops (both the for-loop and the while loop). This assignment also...
Write a program that uses loops (both the for-loop and the while loop). This assignment also uses a simple array as a collection data structure (give you some exposure to the concept of data structure and the knowledge of array in Java). In this assignment, you are asked to construct an application that will store and retrieve data. The sequence of data retrieval relative to the input is Last In First Out (LIFO). Obviously, the best data structure that can...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version...
Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before. Output: Enter the full name of a person that can serve as a reference: [user types: Bob Smith] Bob Smith is reference #1 Would you like to enter another name (Y or N)? [user types: y] Enter...
MATLAB PROBLEM convert the for loop to a while loop. vec= [1 2 3 4 5]...
MATLAB PROBLEM convert the for loop to a while loop. vec= [1 2 3 4 5] newVec= [] for i=vec if i>5 new vec=[newvec, i] end end end
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1...
Use a sentinel while loop that repeatedly prompts the user to enter a number, once -1 is entered, stop prompting for numbers and display the maximum number entered by the user. I am struggling to have my program print the math function. Here is what I have so far: import java.util.*; public class MaxSentinel { public static void main(String[] args) {    Scanner input = new Scanner(System.in); System.out.println("Please enter a value. Press -1 to stop prompt."); int number = 0;...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
1. Write Python program that use a while loop to determine how long it takes for...
1. Write Python program that use a while loop to determine how long it takes for an investment to double at a given interest rate. The input will be an annualized interest rate, and the output is the number of years it takes an investment to double. Note: The amount of initial investment can be any positive value, you can use $1. 2. Write a function def DoubleInvest(initialInvest) to perform the same functionalities as question1. Make sure to include you...
Open Average Test Scores while loop, comment out the while loop and add a for loop...
Open Average Test Scores while loop, comment out the while loop and add a for loop that averages 4 test scores. Code C# While loop code using System; class Program { static void Main() { int count = 0, total = 0, number;    while (count < 3) { Console.Write("Enter a number: "); number = Convert.ToInt32(Console.ReadLine()); total += number; count++; }    double average = total / 3.0; Console.Write("Average = " + average.ToString("####0.00")); } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT