Data and processes in computing
Data and processes in computing

This free course is available to start right now. Review the full course description and key learning outcomes and create an account and enrol if you want a free statement of participation.

Free course

Data and processes in computing

Exercises on Section 5

Exercise 12

Give a full description of a function (using the infix notation ×) corresponding to multiplication of integers.

Answer

Solution

A suitable description of integer multiplication is given below.

function (infix) (x × y on Int) return in Int

pre   true.

post   The returned value is the result of multiplying x and y.

Exercise 13

With n = 966, evaluate each of:

(a) MOD(n, 10);

(b) DIV (n, 10);

(c) MOD(DIV (n, 10), 10).

Answer

Solution

966 = 96 × 10 + 6. So (a) MOD(966, 10) = 6 (the remainder when 966 is divided by 10), and (b) DIV (966,10) = 96. Then (c) MOD(DIV (966, 10), 10) becomes MOD(96, 10). This evaluates to 6, which is the remainder when 96 is divided by 10.

Exercise 14

The function LAST is described below.

function LAST(s in SeqOfX ) return in X

pre   s is not empty.

post   The returned value is the last element in s. For example, LAST([1, 2, 3]) = 3.

Consider the expression NOT(LAST(s) =Char LAST (t)).

  • (a) What is the value of this expression if s is “the” and t is:

    • (i) “same”;

    • (ii) “different”?

  • (b) Under what circumstances does this expression have the value true?

  • (c) Is this expression valid with s = [1,2] and t = [1,8,4,2]?

Answer

Solution

(a) If s = “the” then LAST(s) = ‘e’. (i) With t = “same”, we have LAST(t) = ‘e’, and the given expression becomes

NOT(‘e’ =Char ‘e’) = NOT (true) = false.

(i) With t = “different”, we have LAST(t) = ‘t’, and the given expression becomes

NOT(‘e’ =Char ‘t’) = NOT (false) = true.

(b) The given expression is true if the strings s and t have different last letters.

(c) With these values of s and t, the given expression becomes NOT(2 =Char 2). Now =Char compares two characters, and 2 is an integer. So this expression is not valid. (For the given expression to be valid, we need s and t to be sequences of characters, and to be non-empty, so that the precondition of LAST is satisfied.)

M263_1

Take your learning further

Making the decision to study can be a big step, which is why you'll want a trusted University. The Open University has 50 years’ experience delivering flexible learning and 170,000 students are studying with us right now. Take a look at all Open University courses.

If you are new to university level study, find out more about the types of qualifications we offer, including our entry level Access courses and Certificates.

Not ready for University study then browse over 900 free courses on OpenLearn and sign up to our newsletter to hear about new free courses as they are released.

Every year, thousands of students decide to study with The Open University. With over 120 qualifications, we’ve got the right course for you.

Request an Open University prospectus