kent logo

CO538 Anonymous Questions and Answers Keyword Index

This page provides a keyword index to questions and answers. Clicking on a keyword will take you to a page containing all questions and answers for that keyword, grouped by year.

To submit a question, use the anonymous questions page. You may find the keyword index and/or top-level index useful for locating past questions and answers.

Keyword reference for table


Question 84 (2003):

In our seminar we were given the syntax to (a) create our own data type which is usefull for the coordinates and (b) fill the data type with values.

I can create my own data type with no trouble, but assigning a data type of:

        INT x,y:

the way we were shown:

    [5] COORDS phil.pos[[*,*],[*,*],[*,*],[*,*],[*,*]]: (with the * as INT's)

doesn't work - I have tried messing about to get it to work but to no avail. Any obvious problems with this syntax?

Answer 84:

The syntax is nearly there, but not quite. Constants in occam aren't like variables; they're constants, not variables that you can't assign/input to. Thus the declaration of these is slightly un-expected. As a minor point, `RECORDS' in your data-type definition should just be `RECORD'.

The correct way to declare a set of constant co-ordinates (using your record type) would be:

    VAL []COORDS phil.pos IS [[*,*], [*,*], ...]:

You don't need to give the array-dimension explicitly here -- the compiler can count the number of things on the right-hand side.

If, however, you want to change these values as the program runs, you'd need a real variable array:

    [5]COORDS phil.pos:
      phil.pos := [[*,*], [*,*], ...]

.. or the shorter way using an INITIAL declaration.

See also the answer to Question 68 (2000).

Keywords: record-types , table


Question 68 (2000):

I've forgotten how to implement the lookup table for the screen positions for the dining philosophers. Any help would be appreciated.

Answer 68:

Your animation code should not be highly repetitive with the same logic repeated with different magic numbers - e.g. for displaying new positions of the different philosophers. Having a table of anchor coordinates for each philosopher may help. A table is simply an array of VALues (i.e. constants). For example:

  VAL [][2]BYTE phil.anchor IS [[40, 5], [70, 10], [55, 15],
                                [35, 15], [10, 10]]:

where the [][2]BYTE could have just been [][]BYTE, but being specific about the dimension of the coordinates (rather than the number of them) seems right. We could have been even more explicit and written [5][2]BYTE, but that seems too much. Any way, the compiler will fill in the sizes of any blanks in what we write after the IS.

With the above, it's nice to give names to the two indices of the [2]BYTE array coordinates - e.g.


Then, if we want to move the screen cursor to the anchor point for philosopher i - and we know that that i is a valid philosopher number (i.e. 0 through 4):

    cursor.x.y (phil.anchor[i][X], phil.anchor[i][Y], screen)

or, more efficiently:

    VAL [2]BYTE phil.i IS phil.anchor[i]:
    cursor.x.y (phil.i[X], phil.i[Y], screen)

since it only checks the array index (for out-of-bounds error) and calculates the address of phil.anchor[i] once.

Keywords: q7 , cursor , table , animation

Referrers: Question 43 (2009) , Question 30 (2008) , Question 84 (2003) , Question 29 (2002)

Valid CSS!

Valid XHTML 1.0!

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.
Last modified Mon May 20 13:50:33 2013
This document is maintained by Fred Barnes, to whom any comments and corrections should be addressed.