/*THIS PROGRAM CONTAINS TWO BUGS.  WHAT ARE THEY?*/
#include <stdio.h>

double discriminant(a, b, c)
  {
  return(b*b - 4.0*a*c);
  }

double posroot(a, b, root_det)
  {
  return((-b+root_det)/(2.0*a));
  }

double negroot(a, b, root_det)
  {
  return((-b-root_det)/(2.0*a));
  }

double real_part(a, b)
  {
  return(-b/(2.0*a));
  }

double imaginary_part(a, root_det)
  {
  return(root_det/(2.0*a));
  }

int main()
  {
  double a, b, c, det;
  printf("Ax^2 + Bx + C\nEnter coefficient A: ");
  scanf("%lf", &a);
  printf("Enter coefficient B: ");
  scanf("%lf", &b);
  printf("Enter coefficient C: ");
  scanf("%lf", &c);
  det = discriminant(a, b, c);
  if(det == 0.0)
    printf("root is %f\n", posroot(a, b, 0.0));
  else if(det < 0.0)
    {
    double re, im;
    det = sqrt(-det);
    re = real_part(a, b);
    im = imaginary_part(a, det);
    printf("roots are %f-%fi and %f+%fi\n", re, im, re, im);
    }
  else
    {
    det = sqrt(det);
    printf("roots are %f and %f\n", negroot(a, b, det), posroot(a, b, det));
    }
  return 0;
  }

/*ANSWERS:
0. fails to include <math.h>, so sqrt() defaults to int
1. fails to specify types for formal parameters (using ANSI or original syntax)
HERE IS THE CORRECT VERSION OF THE PROGRAM:*/
#include <stdio.h>
#include <math.h>

double discriminant(a, b, c)
double a, b, c;
  {
  return(b*b - 4.0*a*c);
  }

double posroot(a, b, root_det)
double a, b, root_det;
  {
  return((-b+root_det)/(2.0*a));
  }

double negroot(a, b, root_det)
double a, b, root_det;
  {
  return((-b-root_det)/(2.0*a));
  }

double real_part(a, b)
double a, b;
  {
  return(-b/(2.0*a));
  }

double imaginary_part(a, root_det)
double a, root_det;
  {
  return(root_det/(2.0*a));
  }

int main()
  {
  double a, b, c, det;
  printf("Ax^2 + Bx + C\nEnter coefficient A: ");
  scanf("%lf", &a);
  printf("Enter coefficient B: ");
  scanf("%lf", &b);
  printf("Enter coefficient C: ");
  scanf("%lf", &c);
  det = discriminant(a, b, c);
  if(det == 0.0)
    printf("root is %f\n", posroot(a, b, 0.0));
  else if(det < 0.0)
    {
    double re, im;
    det = sqrt(-det);
    re = real_part(a, b);
    im = imaginary_part(a, det);
    printf("roots are %f-%fi and %f+%fi\n", re, im, re, im);
    }
  else
    {
    det = sqrt(det);
    printf("roots are %f and %f\n", negroot(a, b, det), posroot(a, b, det));
    }
  return 0;
  }

/*Here's another version, which uses static data to avoid the proliferation of
  functions and temporary variables.*/
#include <stdio.h>
#include <math.h>

void printroot(a, b, c)
double a, b, c;
  {
/*if root_det>0: is_complex is 1 for positive discriminant, 0 for negative*/
  static int is_complex = 0;
  static double root_det = -1.0;	/*set when the first root is computed*/
  if(root_det == -1.0)
    {
    root_det = b*b-4.0*a*c;		/*first call, so compute discriminant*/
    if(root_det < 0.0)
      {
      is_complex = 1;			/*if negative, set is_complex*/
      root_det *= -1.0;
      }
    root_det = sqrt(root_det);
    if(is_complex)			/*print negative root*/
      printf("%f-%fi\n", -b/(2.0*a), root_det/(2.0*a));
    else
      printf("%f\n", (-b-root_det)/(2.0*a));
    }
/*on 2nd call, if roots are distinct, print positive root*/
  else if(root_det > 0.0)
    {
    if(is_complex)
      printf("%f+%fi\n", -b/(2.0*a), root_det/(2.0*a));
    else
      printf("%f\n", (-b+root_det)/(2.0*a));
    }
  }

int main()
  {
  double a, b, c;
  printf("Ax^2 + Bx + C\nEnter coefficient A: ");
  scanf("%lf", &a);
  printf("Enter coefficient B: ");
  scanf("%lf", &b);
  printf("Enter coefficient C: ");
  scanf("%lf", &c);
  printf("roots:\n");
  printroot(a, b, c);
  printroot(a, b, c);
  return 0;
  }
