An-Najah Nation University Faculty of Engineering جامعة النجاح الوطنية كلية الهندسة Computer Engineering Department Data Structures and Algorithms (10636211) HW 1 Due to 26/6/2022 Midnight ILOs [3] 10 points [Finding root of a polynomial using Bisection method] Background: Write a C program to find a root of a cubic polynomial 𝑝(𝑥) = 𝑎 𝑥 3 + 𝑏 𝑥 2 + 𝑐 𝑥 + 𝑑 = 0 in the interval [−50,50] if it exists. You program will first read several lines for the coefficients 𝑎, 𝑏, 𝑐, 𝑎𝑛𝑑 𝑑 of different polynomials from a file. Step#1: Locate the interval [𝑥0 , 𝑥1 ] containing the root as follows: • • fix 𝑥0 to -50 . Also initialize x1 to -50 for each value of x starting from -50 to 50 with increment of 1 o If |𝑝(𝑥)| < epsilon -> display the value of x as the root and stop. o If 𝑝(𝑥) ∗ 𝑝(𝑥0 ) < 0 -> assign x to x1 and get outside the loop (using break statement) Step#2: Finding the root: • • • After the loop, if 𝑥1 is still -50 -> display “No root found inside [−50,50] “ and terminate If not -> The root is in the latest interval [𝑥0 , 𝑥1 ]Then apply the following bisection procedure method: o Compute 𝑥𝑚 =(x1+𝑥0 )/2 which represents the middle of the interval [𝑥0 , 𝑥1 ] o While |𝑝(𝑥𝑚 )| >= epsilon ▪ If 𝑝(𝑥0 ) ∗ 𝑝(𝑥𝑚 ) < 0 -> root between x0 and 𝑥1 , so 𝑥1 = xm ▪ 𝑖𝑓 𝑝(𝑥𝑚 ) ∗ 𝑝(𝑥1 ) < 0 -> root between xm and 𝑥1 , so 𝑥0 = xm ▪ update 𝑥𝑚 = (𝑥0 +𝑥1 )/2 Display the value of the root found and the number of iterations (repetitions) to obtain the root. Note: Define epsilon as a constant with a value = 10−6 Homework requirements: • • First, you must ask the user to enter the name of the file. If the file is correct, read the file and produce the result as an output file with the information in the table and the Summary. Ask the user what the name of the output file is to save the data. Make sure the data is wellspaced and organized well in the file. • You must define a data structure called Polynomial, with member variables as above with 𝑎, 𝑏, 𝑐, 𝑎𝑛𝑑 𝑑, no functions. Define an array with the suitable size of the data read from the input file. Make all calculations required to produce the result file. • The user may repeat the process again with a different file, make an option for exit or repeat again. • Don’t use any class! • Define the following functions: o double Find_Root(Polynomial, int&); // returns root if found and number of iterations o o double Min_Root(Polynomial [ ] , int size); // return the minimum root found for number of polynomials. void set_Polynomial(Polynomial&, double, double, double, double); // set the values of variable of type Polynomial structure. The sample data file: The first line contains the number of readings. Each column represents the data entry as explained before. The output result on screen or file: Polynomial # of iterations p(x)=0.5 x^3 + 0.88 x^2 +-1.2 x +7.5=0 27 Root value -3.591302 p(x)=-2.5 x^3 + 45.2 x^2 +7.8 x +55.6=0 35 18.316626 p(x)=0.5 x^3 + 87.8 x^2 +45.2 x +23.4=0 Summary: The minimum root found in the file is -3.591302 Not found