In: Computer Science
What do you understand by pre- and post-conditions
of a function?
Write the pre- and post-conditions to axiomatically specify the
following
functions:
(a) A function takes two floating point numbers representing the
sides of
a rectangle as input and returns the area of the
corresponding
rectangle as output.
(b) A function accepts three integers in the range of -100 and +100
and
determines the largest of the three integers.
(c) A function takes an array of integers as input and finds the
minimum
value.
(d) A function named square-array creates a 10 element array
where
each all elements of the array, the value of any array element
is
square of its index.
(e) A function sort takes an integer array as its argument and
sorts the
input array in ascending order.
Pre-condition is the condition under which function will operate correctly.
Post-condition is the result after function is executed.
Answer a
Assuming function takes two floating point numbers a and b
Pre-condition - a > 0 && b > 0
Post-condition - area == a*b
Answer b
Assuming function takes three integer numbers a, b and c
Pre-condition - a, b and c are in range of -100 and 100
(a >= -100 && a <=100) && (b >= -100 && b <=100) && (c >= -100 && c <=100)
Post-condition -
largest = a (if a is largest)
largest = b (if b is largest)
largest = c (if c is largest)
largest = a (if all are same)
Answer c
Assuming function takes array of integers
Pre-condition - array size must be greater than 0
Post-condition - print minimum element
Answer d
Pre-condition - no pre-condition as there is no condition to be true for its execution.
Post-condition - create array of 10 elements with having value as square of index.
Answer e
Pre-condition - array size must be greater than 0
Post-condition - sorted array in ascending order