text: Find the root of given non decreasing function between A and B | C ++ program for the above approach ; Given function ; Function to find the root of the given non - decreasing Function ; To get the minimum possible answer for the root ; Find mid ; Search in [ low , x ] ; Search in [ x , high ] ; Return the required answer ; Function to find the roots of the given equation within range [ a , b ] ; If root doesn 't exists ; Else find the root upto 4 decimal places ; Driver Code ; Given range ; Function Call
code: #include <bits/stdc++.h> NEW_LINE using namespace std ; #define eps  1e-6 NEW_LINE double func ( double a , double b , double c , double x ) { return a * x * x + b * x + c ; } double findRoot ( double a , double b , double c , double low , double high ) { double x ; while ( fabs ( high - low ) > eps ) { x = ( low + high ) / 2 ; if ( func ( a , b , c , low ) * func ( a , b , c , x ) <= 0 ) { high = x ; } else { low = x ; } } return x ; } void solve ( double a , double b , double c , double A , double B ) { if ( func ( a , b , c , A ) * func ( a , b , c , B ) > 0 ) { cout << " No ▁ solution " ; } else { cout << fixed << setprecision ( 4 ) << findRoot ( a , b , c , A , B ) ; } } int main ( ) { double a = 2 , b = -3 , c = -2 , A = 0 , B = 3 ; solve ( a , b , c , A , B ) ; return 0 ; }