If you were to measure the circumference around any circle and the diameter accros the same circle, and then divide the circumference by the diameter, you might be amazed to find that your answer is the same for every circle you measure, about 3.14159265358973323. This is such an amazing result, that mathematicians have given this result a name, the Green character PI.

Assuming you've accurately measured a circle, you might also be amazed to find out that your division never ends since you can always achieve a better preceision to the right of the decimal for this calculation. Furthermore, it has been proved that there is no repeating pattern for the digits to the right of the decimal. This type of number is referred to as an irrational number by mathematicians.

Various formulas exist for calculating the value of Pi. One approach is to calculate the value of the following series, the more terms used in the series, the better the precision of Pi that is calculated.

Pi = 4 - 4/3 + 4/5 - 4/7 + 4/9 - 4/11 + ...

As you might have noticed, calculating the value of this series only requires performing elementary arithmetic. Notice that the terms in the denominator consist of increasing odd numbers and each subsequent term alternates between being added to or subtracted from the previously summed terms. For example, the value of Pi using the first three terms in this series is:

Pi = 4 - 4/3 + 4/5 = 3.46666

The more terms that are used, the closer the approximation will become to Pi's actual value.

Requirements:

Write a program based on the previous series that calculates the value of PI given a specified number of terms.

  • Tell the user what the program does before prompting for input.
  • User double precision floating point values to store all floating point values.
  • Write a user-defined function to read input from the user. User-defined function requirements are:
    • The prompt for user input will be stored in a string type variable and passed as an input argument to the function (example: "Enter the number of terms to use").
    • The function will prompt the user for the input and then use a loop to verify that the user input is positive and non-zero. If it is not, the function will issue an error message and then re-prompt the user to enter the number again, until a positive, non-zero value is entered.
    • Once valid input has been entered, the value that the user entered will be returned to the main function.
  • Call the above user-defined function to read in the number of terms in the series to be used for calculating the value of Pi (note, the above two formula examples have 6 and 3 terms respectively). Pass in the prompt for terms and return the validated number of terms value to main.
  • After a valid number of terms has been entered, call the same user-defined function a second time, to read in a display count representing how often the program should display the current total value of the series. For example, if the user inputs 1, the result after the 10th, 20th, 30th, etc. calculations (see sample outputs below). Using the same user defined function will work, because the user input should again be positive and non-zero. Pass in the prompt for steps and return the validated step value to main.
  • Output the total value of the series at each step specified by the display count, displayed to 9 decimal places.
  • Output the final value of Pi for the last sum in the series, to 9 decimal places. See sample output 1. See sample output 2.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.