News flash: I'm going to Australia in February, so this Web site may be switched off at any time.


A quadratic root solver in Icon

Consider the problem of solving a quadratic equation, ax^2 + bx + c = 0: there may be two, one, or no roots. So we can define a function that generates 2, 1, or 0 results. Here is a main program that uses such a function to solve x^2-4x+3=0:

procedure main ()
  local root      # Holds each root generated by the solver
  local count     # Counts the number of roots returned

  count  :=  0;

  every  root := Quadratic_Roots ( 1, -4, 3 )  do
  { #-- The braces {} group the next two statements together
    write ( "One solution is: x = ", root );
    count  +:=  1;        # Count the roots generated
  }
  write ( "The number of solutions was ", count );
end
The output of this program is:
One solution is: x = 3.0
One solution is: x = 1.0
The number of solutions was 2

Next: A quadratic root function in Icon
See also: Using the Icon programming language
Previous: Using generators in Icon

John W. Shipman, john@nmt.edu