next up previous
Up: APS105S Tuesday Quiz 1 Previous: 2 Recursive Functions

3 Finding the Zero Crossings of a Function

You are to complete a program, given below, that counts the number of times that a function f crosses the X-axis in a range of X values specified by the user. In essence, the program counts the number of zeroes of the function in a given range. The function f is defined as follows:

Below is a plot of the function f:

 figure45

For example, if the input range was specified as 0.0 to 8.0, the output of the program should be 2, because the function f crosses the x-axis twice between 0 and 8, as indicated by the crosses in the figure.

The file /usr/copy/aps105/countzero.t contains the code that does most of this, and is given below. There are two modules that do not exist: Func, and Cross. The Func module calculates the value of the function f, as defined above. The Cross module takes two values of the function as input and returns a boolean value that indicates if an x-axis crossing has occured between those two values.

var Start, End, Scan, Previous, Current: real
var NumCross: int

get Start,End

NumCross := 0
Previous := Func(Start)
Scan := Start

loop
    Scan := Scan + 0.7
    exit when Scan > End

    Current := Func(Scan)

    if Cross(Previous,Current) then
        NumCross := NumCross +1
    end if

    Previous := Current
end loop

put "Number of zero crossings between ", Start, " and ", End, " is ", NumCross

You are to complete the program by writing the Turing modules Func and Cross.

You may include the code of countzero.t either by copying from the file /usr/copy/aps105/countzero.t into your file ``part3.t'' or by simply having the statement include "/usr/copy/aps105/countzero.t" after the declaration of your modules.

Restrictions:

The modules that you write are not to output anything. You may not change the code you import from countzero.t in any way.

A solution


Jonathan Rose
Sat Mar 8 17:24:49 EST 1997