// 09162009a.cpp, P H Anderson, Sept 16, '09

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float solve_eq(float start, float stop, int num);
float f_eval(float x);

int main()
{      
    float sol;
    
    sol = solve_eq(0.3, 2.0, 100);
    
    if (sol < 0.0)
    {
        printf("uh-oh\n");
        goto DONE;
    }
    
    printf("%.6f\n", sol);
        
    sol = solve_eq(sol - 0.1, sol + 0.1, 100);
    
    if (sol < 0.0)
    {
        printf("uh-oh\n");
        goto DONE;
    }
    
    printf("%.6f\n", sol);
    
    sol = solve_eq(sol - 0.01, sol + 0.01, 100);
    
    if (sol < 0.0)
    {
        printf("uh-oh\n");
        goto DONE;
    }
    
    printf("%f\n", sol);
DONE:               
    system("pause");    
}

float solve_eq(float start, float stop, int num)
{
    float x1, x2, y1, y2;
    for(int n=0; n<num; n++)
    {
        x1 = start + (stop - start) * n / num;
        x2 = start + (stop - start) * (n+1) / num;
        
        y1 = f_eval(x1);
        y2 = f_eval(x2);
        
        if ((y1 >=0) && (y2 < 0))
        {
            return((x1+x2) / 2.0);
        }   
        else if ((y1 <=0) && (y2 > 0))
        {
            return((x1+x2) / 2.0);
        }   
        else
        {
            continue;
        }
    }
    return(-1.0); // no solution found
}

float f_eval(float x)
{
    float y;
    y = exp(x) - x - 1.5;
    return(y);
}

#ifdef OUTPUT
0.852500
0.857500
0.857600
Press any key to continue . . .
#endif