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 magic-numbers
2012 |
Submission reference: IN2191
I'm trying to write a display process for q7 so I can get it working, but I'm completely stuck at this very early stage....
The lectures seemed to suggest that this could be done with a replicated ALT running across all 11 items in the channel array, but I don't know how to do this as:
ALT i = 0 FOR SIZE in?
will do it for all the items in the array ... but, if I want to differ my messages for the security guard compared to the forks and the philosophers, I will need to have a nested replicated ALT that is FOR (SIZE in? - 1) and then the 11th one for the security guard will be a normal guard under that?
Alternativley, I could rethink the whole thing and do it using a different numbering system on the report channel but then I read Question 31 (2011) and was totally confused by what is meant by:
"The philospher that picks up fork id on its right channel is philospher (id + 1)\n.phils."
Surely if philosopher 1 picks up fork 1 on his right then philosopher (1 + 1) / 5 is 0?
What is the suggested way for writing the display process at this early stage? Is a replicated ALT the best / only way or is writing out each and every statement as a guard acceptable (as this is the only way I can think of getting it to work)?
If you have an array of 11 report channels (as in "Applying" slides 64-65), then the simplest way to handle them in the display process is via a replicated ALT.
To work out whether the report is from the security guard, a fork or a philosopher, you don't need to nest a slightly less replicated ALT (listening to the first 10 channels from the phils and forks) within an ALT that listens also to the 11th (from security). Nor do you have to replicate the guarded processes 11 times by hand.
Look at "Choice" slide 116 and suppose that its n is 11. In the response to the guard, just test the value of i (e.g. using an IF). If i is between 0 and 4 inclusive, it's a phil report. If i is between 5 and 9 inclusive, it's a fork report. If i is 10, it's a security report.
Of course, your code should not contain magic numbers like 4, 5, 9 and 10. Those numbers depend on how many phils there are (which equals the number of forks) and that is a named constant, n.philosophers, given in your starter code file. Instead, use relevant formulae (n.philosophers - 1, 2*n.philosophers etc.). Then, should anyone wish to change the number of phils in the system, only that constant has to be changed – they don't need to hunt down all the magic numbers that depend on it.
NO CODE SHOULD HAVE MAGIC NUMBERS other than 0 and 1 (and only then if they don't depend on some system value and are 0 or 1 by chance) – except, of course, when declaring names for them. The rest of the code should use the names (or formulae that use the names). This is just good engineering.
You have mis-read the answer to Question 31 (2011). The formula is:
(id + 1)\n.phils
where the "\" symbol means modulo, not divide. So, if the fork id is 1, then the philospher who has picked it up (on the fork's right? channel) has the number given by the above formula:
(1 + 1)\5
which is 2\5, which is 2.
Looking at "Applying" slide 29 (referenced in Question 31 (2011)), let's assume that the code gives the phil and fork being replicated an id equal to the replicator control value i. In that case, the fork with an id of 1 is connected to channel-ends left[1]? and right[2]? and is the fork in position 4 o'clock in the network diagram. The phil connected to that fork's right channel is at position 5 o'clock in the diagram and is the phil with id value 2. So, the above formula got it right! ;)
Keywords: q7 , magic-numbers
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. |