Computer Science 3 - 2016 - Test on Week 2

This should take no longer than 25 minutes and is intended to probe whether you have understood Practical 2 and the PVM. Use your textbook - but not your notes or solutions - if you think it will help.

1. I have a memory like my friend Ερατοσθενes and his σιεϕ and cannot remember - remind me of your first name, your (surname) and student number [1]

2. Suppose Parva and the PVM were extended to provide two simple functions - max() and sqr() - which would you allow you easily to translate code like

           int a = max(b, c);
           int s = sqr(a);

and so on.

(a) What changes would you have to make to the switch statement in the PVM emulator in the Assembler system you have been using to support two additional opcodes that could provide this functionality? [6]

(b) Would you have to make any other changes to the PVM.cs file (that is, besides the main switch statement) to support the extension? Explain your thinking! [4]

3. From those dim distant maths classes at school you should recall the geometrical theorem of another Great Greek, one Pythagoras, to the effect that if the lengths of the sides of a right angled triangle are denoted by a, b and h (h being the hypotenuse - the side opposite to the right angle) then a2 + b2 = h2.

The following Parva program takes it upon itself to read triples of numbers and determine whether they have this property.

     void main () {
     // Test triples of numbers to see if a right angled triangle could be constructed from them
     // P.D. Terry, Rhodes University, 2016
       int a, b, c;
       read(a);
       while (a > 0) {
         read(b, c);
         write(a, b, c);
         if (2 * sqr(max(max(a, b), c)) == sqr(a) + sqr(b) + sqr(c))
           writeLine(" form a right angle triangle");
         else
           writeLine(" do not form a right angle triangle");
         read(a);
       }
     }

(a) Explain the logic behind the rather complicated condition used in the if statement in this program. [4]

(b) Translate this program above into PVM code, making use of your new opcodes, and also of the STL and LDL opcodes you should have implemented during the practical. (Hint: this can be done in about 44 lines, but once you see the patterns it should be easy to write out the code.) [25]


Home  © P.D. Terry