I have my code below that lets the user inputs the order and the corresponding coefficients of a polynomial. This code will run but it is totally wrong..A sample run is shown below. The users polynomial is x^2 - 5x + 2.
The general rule is, There is sign change if:
f(guess1) * f(guess2) < 0
This polynomial above has a sign change at initial guesses 4 and 5 since
f(5) = 5^2 - 5(5) + 2 = 2
f(4) = 4^2 - 5(4) + 2 = -2
f(4) * f(5) < 0.
But it turns out that my program is always saying that there is no sign change!. PLEASE HELP ME WITH THIS!!! Thanks!!!!!
Enter The power of the polynomial: 2
Enter the coefficient of x^2: 1
Enter the coefficient of x^1: -5
Enter the constant term: 2
Enter first initial guess: 4
Enter second initial guess: 5
No root is bracketed!
Enter first initial guess:
.
.
.
You left s unitialized in the f function. Try double s = 0;
After changing that right around line 45, I would suggest outputting information on what value1 and value2 is. Just to be certain the f function is working.
At line 81 double s; the variable s is not initialised, it contains a garbage value. Since later at line 85, other values are added to s, the initial value of s is vitally important. In this case, it should be assigned an initial value of zero. double s = 0.0;