Question
Use C++ to find the roots of any polynomial of order 0 < order
Use C++ to find the roots of any polynomial of order 0 < order <= 4.
You need to input the values of five coefficients. If the polynomial has an order less than 4 then 0 coefficients will be used to express the polynomial as a polynomial of degree 4. Your coefficients will be read from an input file. You will print the results of each iteration to an output file. You will check some inputs to determine they are the correct type and that = they are in range.
Your main program must use defined constants (not literal constants) for all limits on variables, and for all numerical constants. Your code must do each of the following:
1. Prompt for and read a value of the initial estimate to the root for the first line of the table. -8.0 <= estimate <= 8.0
2. If the initial estimate is not a floating point number print the following error message: The initial estimate was not a number
3. If the initial estimate is not in range print the error message (this test should be done after testing has determined a floating point number was read).
4. If errors occur in either step 2 or 3 then re-prompt the user for the initial estimate, then re-read the initial value. You should not re-prompt the user more than 3 times and you should complete reading and testing the values of the initial estimate before reading the increment.
5. Prompt for and read the increment between the initial estimate on line n and the initial estimate on line n+1 using: 0.1 <= increment <= 4
6. If the increment is not a floating point number print the following error message: The increment was not a number
7. If the increment is not in range print the error message (this test should be done after testing has determined a floating point number was read)
8. If errors occur in either step 6 or 7 then re-prompt the user for the increment, then re-read the value of the increment. You should not re-prompt the user more than 3 times and you should complete reading and testing the values of the increment before reading the coefficients.
9. Then calculate the number of rows
10. The five coefficients will be read from an input file. Each coefficient should be within the range -20 <= coefficient <= 20
11. Prompt for and read a string variable. This variable will hold the name of the file to open. Use the prompt: Enter the name of the input file containing coefficients :
12. Open the file, check the file opened correctly. If it did not print the error message below and terminate the program.
Cannot open coefficient file
13. Read and check the value of each coefficient. The coefficient of x4 should be read first, then x3, then x2 then x1 then x0. One coefficient should be read and tested before the next coefficient is read
14. For each coefficient check that
a) There is a value in the file for the coefficient
b) That the value read is in range.
If either of these tests fail print the appropriate error message. Then terminate the program. The appropriate error messages are shown below.
The value of the coefficient for x**N is not in the input file
The value of the coefficient for x**N is not in range
Please note that the N in each of the messages above should be the exponent for the coefficient being read. It should be 3 for the coefficient of X cubed and 2 for the coefficient of x squared and so on
15. Close the input file
16. Read the name of the output file into a string variable
17. Open the output file and test if it opened correctly.
18. Print two blank lines to the screen then print the titles for the table to be printed to the screen.
19. For each row of the table calculate the approximations to the root of the polynomial using Newtons method, adding the output to the output file as described below
a) Print titles to the table in the output file. Each title is placed in a field 16 characters wide. The titles (in order) are it# and X value. The titles should be right justified
b) Go to the next line of output in the output file and print 0 (for iteration number) and the value of the initial X estimate. Both numbers should be in a field 16 characters wide. The value of X0 should have 9 digits after the decimal point.
c) Go to the next line in the output. N should now be 1
d) Calculate XN
e) Print the number of iterations that have been completed (N) to the output file in a field 16 characters wide
f) Print the value of XN to the output file in a field 16 characters wide with 9 digits after the decimal point. Also print the value of XN to the screen if N=1, N=2, or N=3)
g) Move to the next line in the output file
h) Check to see if the estimate of the value of the root has converged, |Xn-Xn-1|< 0.00001
i) If the value of the root has converged
Print the following message to the output file then print two blank lines to the output file
The value of the root is xx.xxxxx
The root took NN iterations to calculate
The value of xx.xxxxx should be replaced with the value of the root and the value of NN should be replaced with the number of iterations the root took to calculate. Print two more blank lines.
Exactly as in problem 1 print the number of iterations and the converged root to the table on the screen, then move to the next line of output
Repeat step 19 for the next line of the table
j) If the value of the root has not converged increment N. If N <= 30 go to step 19d otherwise
Print the following message to the output file then print two blank lines to the output file
DID NOT CONVERGE
Complete printing the line to the screen
Move to the next line of output on the screen
20. Close the output file
21. Exit the program.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started