The third part is the desired input-output relation. It is written as a comment which starts with — outputs:. The input and output can be written using English and mathematical notation. Example .
. Write the specification of an algorithm for computing the square root of a number. Chapter Page - - . Let us name the algorithm .
. It takes the number as the input. Let us name the input n. n should not be negative.
. It produces the square root of n as the output. Let us name the output y. Then n should be the square of y.
Now the specification of the algorithm is (n) -- inputs: n is a real number, n ≥ . -- outputs: y is a real number such that y = n. . .
Specification as contract Specification of an algorithm serves as a contract between the designer of the algorithm and the users of the algorithm, because it defines the rights and responsibilities of the designer and the user. Ensuring that the inputs satisfy the required properties is the responsibility of the user, but the right of the designer. The desired input-output relation is the responsibility of the designer and the right of the user. Importantly, if the user fails to satisfy the properties of the inputs, the designer is free from his obligation to satisfy the desired input-output relation.
Right User Input - output relationship Responsibility Algorithm (designer) Property of inputs Responsibility Right Figure . : Input property and the input- output relation as rights and responsibilities Example . . Consider the specification of the algorithm .
(n) -- inputs: n is a real number, n ≥ . -- outputs : y is a real number such that y = n. The algorithm designer can assume that the given number is non-negative, and construct the algorithm. The user can expect the output to be the square root of the given number.
The output could be the negative square root of the given number. The specification did not commit that the output is the positive square