Next / Previous / Index / ITC Help System / Publications / Site map / NM Tech homepage

A quadratic root solver in Icon

Tech Computer Center logo

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 );
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: A tutorial for the Icon programming language
Previous: Using generators in Icon
Site map
Index: Keyword index to help pages
Help: New Mexico Tech Information Technology and Communications: Help System
ITC Publications
To report a problem: File a ticket
Send mail to the User consultant on duty or call them at 575-835-5437
Home: About New Mexico Tech

John Shipman,
Last updated: 1996/01/06 21:12:57 UT
QR two-dimensional bar code