## 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 ); endThe output of this program is:

One solution is: x = 3.0 One solution is: x = 1.0 The number of solutions was 2

Site map

To report a problem:

Send mail to the

John Shipman, john@nmt.edu
Last updated: 1996/01/06 21:12:57 UTURL: http://www.nmt.edu/tcc/help/lang/icon/quadratic.html |