Always no sign change

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:
.
.
.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  #include <iostream>
#include <math.h>
using namespace std;

double coeff[1000];
int p;

double f(double);

int main()
{
    
    
    double guess1, guess2, value1, value2;
    
    
    cout << "Enter the power of the equation: ";
    cin >> p;
   //Get Function
   for(int i = p; i > 0; i--)
   { 
           cout << "Enter the coefficient of x^" << i << ": ";
           cin >> coeff[i];
   }
   
           cout << "Enter the constant term: ";
           cin >> coeff[0];
           
           cout << endl;
   
   //Sign Change
 
   
   
   do
    {
   cout << "Enter first initial guess: ";
   cin >> guess1;
   cout << "Enter second initial guess: ";
   cin >> guess2;
    

    value1 = f(guess1);
    value2 = f(guess2);
    
    if(value1 * value2 < 0)
    {
              cout << endl;
              cout << "A root has been passed!";
              cout << endl;
    }
    else
    {
        cout << endl << endl;
        cout << "No root is bracketed! "
             << "Increase delta x or find another guess!";
             
        cout << endl << endl;
        

    }
    }while(!((value1 * value2) < 0));
              
              
   
           
   
   
    
    
    
    
    cin.get();
    cin.get();
    
    return 0;
}
double f(double x)
{
       int i;
       double s;
       
       for(i = 0; i <= p; i++)
       {
             s += coeff[i] * pow(x, i);
       }
       
       return s;

}
    
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.
Last edited on
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;
Thank you so much.! You are all great!.. Problem solved!..
Last edited on
line 81 is a problem, initialise s, don't assume it will be 0.0 by default.
Topic archived. No new replies allowed.