// 02052008.cpp, P H Anderson, Feb 5, '08
//
// Solve for roots  x^2 - 5x + 1 = 0

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

#define FALSE 0
#define TRUE 1

int main()
{
    int n, solution_found;
    int dice, counter;
    float x1, x2, y1, y2;
    float percent;
    
    solution_found = FALSE; // look for solution in range 0.0 to 1.0
    for (n=0; n<1000; n++)
    {
        x1 = 0.0 + 1.0 * n / 1000;
        x2 = 0.0 + 1.0 * (n+1) / 1000;
        y1 = x1 * x1 - 5 * x1 + 1;
        y2 = x2 * x2 - 5 * x2 + 1;
        if ((y1<0.0) && (y2 > 0.0))
        {
            printf("Solution!  %.5f %.5f %.5f %.5f\n", x1, y1, x2, y2);
            solution_found = TRUE;
        }
        else if ((y1>0.0) && (y2 < 0.0))
        {
            printf("Solution!  %.5f %.5f %.5f %.5f\n", x1, y1, x2, y2);
            solution_found = TRUE;
        }  
    }
    if (solution_found == FALSE)
    {
        printf("No solution found.\n");
    }
    
    solution_found = FALSE; // look for solution in range 4.0 to 5.0
    for (n=0; n<1000; n++)
    {
        x1 = 4.0 + 1.0 * n / 1000;
        x2 = 4.0 + 1.0 * (n+1) / 1000;
        y1 = x1 * x1 - 5 * x1 + 1;
        y2 = x2 * x2 - 5 * x2 + 1;
        if ((y1<0.0) && (y2 > 0.0))
        {
            printf("Solution!  %.5f %.5f %.5f %.5f\n", x1, y1, x2, y2);
            solution_found = TRUE;
        }
        else if ((y1>0.0) && (y2 < 0.0))
        {
            printf("Solution!  %.5f %.5f %.5f %.5f\n", x1, y1, x2, y2);
            solution_found = TRUE;
        }  
    }
    if (solution_found == FALSE)
    {
        printf("No solution found.\n");
    }
    
    /////////////////////////////////// roll dice 25000 times, count number of 2's
    counter = 0;
    for (n=0; n<25000; n++)
    {
        dice = rand()%6 + rand()%6 + 1 + 1;
        if (dice == 2)
        {
            ++counter;
        }
    }
    percent = 100.0 * counter / 25000;
    printf("%i %.5f %.5f\n", counter, percent, 100.0 * 1.0 / 36.0);
                
    while(getchar() != 'x')
    {
    }
}
#ifdef XXX
Solution!  0.20800 0.00326 0.20900 -0.00132
Solution!  4.79100 -0.00132 4.79200 0.00326
663 2.65200 2.77778
#endif