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

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...
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)
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")); } }
Cpp challenge Description The purpose of this challenge is to use the WHILE loop to control...
Cpp challenge Description The purpose of this challenge is to use the WHILE loop to control program flow. This challenge will use the WHILE loop in various ways. Requirements Write all your code for the following steps in one file. Ask the user to enter a single string. Ask the user to enter an integer value for a variable called repetitions. Use a while loop to display this string (of your choice) repeated repetitions times, one string per line Use...
1. is sin(pi/4) causal? 2. is sin(pi/4) stable? 3. is delta(n+1) causal? 4.  = ? 5. If...
1. is sin(pi/4) causal? 2. is sin(pi/4) stable? 3. is delta(n+1) causal? 4.  = ? 5. If function w [ n ] is convolved with , what will the result be? 6. if a system with signal length 4 is convolved with its own system response, what will the length of that signal be? 7. In an LTI system, x[n] * h[n]= y[n]. What is x[n-3] * h[n-2] =?
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a...
Python Exercises = Sentinel Values and While Loops #Exercise 1 #Write a while loop with a sentinel value #This while loop ends when the user enters a 1, the sentinel value #Assign the number 8 to a variable which will serve as the sentinel value #Condition: while variable is not equal to 1 #Action: display the number assigned to the variable #Use an input statement (no prompt) to ask the user for a number and assign this number to the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT