Computer Science 301 - 2003

Programming Language Translation


Practical for Week 19, beginning 25 August 2003 - Solutions

The submissions received were very varied, and on the whole of rather disappointing quality. I gained the impression that several groups had left things far too late, or had simply not bothered. On the other hand, there was some innovative work handed in, but even here there was evidence that people had missed several important points. You can find source versions of the program solutions in the solution kit PRAC19A.ZIP on the server.

Some general comments:

(a) You should always put your names and a brief description of the program into the source code.

(b) Several submissions had no commentary at all, and this is just unacceptable.

(c) The pracs in this course are deliberately set to extend you, in the hope that you will learn a lot from each one. Their completion requires that you apply yourself steadily throughout the week, and not just on Thursday afternoon and the following Thursday morning!


Tasks 2 to 7 - The Sieve of Eratosthenes

The first tasks were fairly straightforward, though several groups obviously had not bothered to see whether the extended sieve programs would execute properly. Because the Pascal compilers only use 16-bit arithmetic they appear to allow sieve sizes of about 65000 (Turbo Pascal) and even larger (Free Pascal). But since they both regard an integer as a signed 16-bit number, an extra limitation is imposed - an array indexed by an integer cannot have an upper subscript greater than 32767. It's even worse than that: Consider the code

            K := I (* now cross out multiples of I *);
            REPEAT
              Uncrossed[K] := FALSE; K := K + I
            UNTIL (K > N);

When I becomes large enough K+I becomes larger than 32767, but the overflow means that it appears to become negative. This happens for the prime number 16411, so that the maximum effective sieve with the code above is really only 16410.

We can extend the range of the algorithm by a trick which I did not expect you to discover, but which may be worth pointing out. We replace the above code by

            K := I (* now cross out multiples of I *);
            REPEAT
              Uncrossed[K] := FALSE; K := K + I
            UNTIL (K > N) OR (K < I)

which looks ridiculous, but when K gets too large and then overflows to become negative it will appear to be less than I again.

Much the same sort of behaviour happens in the 16-bit Modula-2 compiler. The type CARDINAL used in the Modula-2 code is implemented as an unsigned 16-bit number, so it might at first appear that we can use a sieve of about 65000 elements. However, when we reach the prime number 32771 the value of K + I overflows. This time we can still use a coding trick like that above, but we have to run the compiler in "optimized" mode to suppress the overflow detection that it normally provides. All rather subtle - up till now you probably have not really written or run code that falls foul of overflow or rounding errors, but they can be very awkward in serious applications.

The 32-bit compilers don't have this problem, but, of course, the amount of real memory available to them is limited, and I found the limits to be approximately those shown. I did not find the real limit for the C# system.

There were several specious reasons thought up to explain why the executables were of such differing sizes. It is not true that this is a function of the sieve size to any marked degree, as the code and data areas are handled separately. The real reasons are that the efficiency of code generation differs markedly from one compiler to another, and that some implementations build in a great deal more extraneous code than others - you could see this in the smaller executable when some compilers were run in "optimizing" mode. The C and C++ executables differ enormously in size - no doubt due to the vast amounts of code needed to support the iostream library.

The Borland 5.5 and Microsoft C/C++ compilers are 32-bit ones, rather than 16-bit ones. But even allowing for this, they suffer from bizarre code bloat for small applications. There may be command line parameters and options that one can set to try to produce tighter code, but I have not bothered to experiment further. Some (most) of the overheads may relate to the fact that they have to produces "Windows" compatible programs. Compilers like this are clearly designed with an "everyone has 256MB of memory and 60GB of disk space, and if they don't they should go and buy more" philosophy.

On the other hand, the limitation imposed by the Parva system is entirely due to the fact that the interpreter system only allows for about 50000 words of memory - which has to hold the pseudo-code and all variables. The limit on the sieve size was about 49000. This could have been extended simply by modifying the interpreter, and then recompiling it, but you were not in a position to do that.

  .---------------------------------------------------------------------------.
  | TASK 2          | Sieve in Pascal              |                          |
  |-----------------+------------------------------+--------------------------|
  | Turbo Pascal    | Code size    3184            | Sieve limit   16410      |
  |-----------------+------------------------------+--------------------------|
  | Free Pascal     | Code size    15872           | Sieve limit   16410      |
  |-----------------+------------------------------+--------------------------|
  | Optimized       |                              |                          |
  | Turbo Pascal    | Code size    3104            | Sieve limit   16410      |
  |-----------------+------------------------------+--------------------------|
  | Optimized       |                              |                          |
  | Free Pascal     | Code size    14848           | Sieve limit   16410      |
  `---------------------------------------------------------------------------'

  .---------------------------------------------------------------------------.
  | TASK 3          | Sieve in Modula-2            |                          |
  |-----------------+------------------------------+--------------------------|
  | JPI Modula-2    | Code size    18609           | Sieve limit   32770      |
  |-----------------+------------------------------+--------------------------|
  | Optimized       |                              |                          |
  | JPI Modula-2    | Code size    18549           | Sieve limit   32770      |
  `---------------------------------------------------------------------------'

  .---------------------------------------------------------------------------.
  | TASK 4          | Sieve in C or C++            |                          |
  |-----------------+------------------------------+--------------------------|
  | Borland C 5.5   | Code size    66560           | Sieve limit   258000     |
  |-----------------+------------------------------+--------------------------|
  | Microsoft C     | Code size    32768           | Sieve limit   258000     |
  |-----------------+------------------------------+--------------------------|
  | Borland C++ 5.5 | Code size    149504          | Sieve limit   258000     |
  |-----------------+------------------------------+--------------------------|
  | Microsoft C++   | Code size    40960           | Sieve limit   258000     |
  `---------------------------------------------------------------------------'

  .---------------------------------------------------------------------------.
  | TASK 5          | Sieve in Java                |                          |
  |-----------------+------------------------------+--------------------------|
  | Jikes/Java      | Code size    N/A             | Sieve limit   61 Million |
  `---------------------------------------------------------------------------'

  .---------------------------------------------------------------------------.
  | TASK 6          | Sieve in C#                  |                          |
  |-----------------+------------------------------+--------------------------|
  | C#              | Code size    28672           | Sieve limit   Enormous?  |
  `---------------------------------------------------------------------------'

  .---------------------------------------------------------------------------.
  | TASK 7          | Sieve on Parva               |                          |
  |-----------------+------------------------------+--------------------------|
  | Parva           | Code size    N/A             | Sieve limit   49000      |
  `---------------------------------------------------------------------------'

  .---------------------------------------------------------------------------.
  | Task 8          | Sieve in Modula -> C/C++     |                          |
  |-----------------+------------------------------+--------------------------|
  | Borland C 5.5   | Code size    62976           | Sieve limit   32770      |
  |-----------------+------------------------------+--------------------------|
  | Microsoft C     | Code size    45056           | Sieve limit   32770      |
  `---------------------------------------------------------------------------'


The Sieve in Parva

The corrected code is very simple:

  void main() {
  // Sieve of Eratosthenes for finding primes 2 <= n <= 4000 (Parva version)
  // P.D. Terry,  Rhodes University, 2003
    const Max = 4000;
    bool[] Uncrossed = new bool[Max];         // the sieve
    int i, n, k, it, iterations, primes = 0;  // counters
    read("How many iterations? ", iterations);
    read("Supply largest number to be tested ", n);
    if (n > Max) {
      write("n too large, sorry");
      return;
    }
    it = 1;
    while (it <= iterations) {
      primes = 0;
      write("Prime numbers between 2 and " , n, "\n");
      write("-----------------------------------\n");
      i = 2;
      while (i <= n) {                          // clear sieve
        Uncrossed[i-2] = true;
        i = i + 1;
      }
      i = 2;
      while (i <= n) {                          // the passes over the sieve
        if (Uncrossed[i-2]) {
          if (primes - (primes/8)*8 == 0)
            write("\n");                        // ensure line not too long
          primes = primes + 1;
          write(i, "\t");
          k = i;                                // now cross out multiples of i
          Uncrossed[k-2] = false;
          k = k + i;
          while (k <= n) {
            Uncrossed[k-2] = false;
            k = k + i;
          }
        }
        i = i + 1;
      }
      it = it + 1;
      write("\n");
    }
    write(primes, " primes");
  }


Task 8 - High level translators

Several students complained that the C code generated by the X2C system was "unreadable", and "not what we would have written ourselves". There can be no dispute with the second of those arguments, but if you take a careful look at the generated C code, it is verbose, rather than unreadable, because long identifier names have been used. This is actually not such a bad thing - at least one can tell the "origin" of any identifier, since the original module in which it was declared is incorporated into its name, and this is is a very useful trick, both for large programs, and (more especially) when one has to contend with the miserable rules that C has for controlling identifier name spaces. In fact, in the days when I used Modula regularly, I used a convention similar to this of my own accord, and have carried it over into my other coding, as you will see in several places in the book. Some of the other "unreadability" presumably relates to the fact that the X2C system is obliged to translate the CARDINAL type to the unsigned int type, which is not one some of you will ever have used - this explains all those funny casts and capital Us that you saw. Some people commented that "maintaining the C code generated would be a nightmare". Well, maybe, but the point of using a tool like this is that you can develop and maintain your programs in Modula-2 and then simply convert them to C when you want to get them compiled on some other machine.


Task 9 The N Queens problem

It was very easy to discover how many solutions can be found to the N Queens problem. Some people noticed that the number of distinctly different solutions would have been lower, as the total included reflections and rotations as though they were distinct.

      2     3     4     5     6     7     8     9      10     11      12
   .-----------------------------------------------------------------------.
   |  0  |  0  |  2  | 10  |  4  | 40  | 92  | 352  | 724  | 2680  | 14200 |
   `-----------------------------------------------------------------------'

Clearly the number of solutions rises very rapidly. Some groups rashly put forward unjustified claims that the number of solutions rose "exponentially". If you were very keen you might like to try to estimate the O(N) behaviour rather better from these figures (how?).


Task 10 - How fast/slow are various implementations?

The times (seconds) taken to execute the sieve program and the Queens programs are shown below (run on my lap top, on top of Windows XP).

  .-------------------------------------------------------------------.
  | Sieve:   Number of iterations  5000    |  Size of sieve  16000    |
  |----------------------------------------+--------------------------|
  | Queens:  Number of iterations  50      |  Board Size     11       |
  |----------------------------------------+--------------------------|
  |                          |  Sieve      |   Queens    |   Queens1  |
  | Turbo Pascal             |  29.9       |   18.8      |   16.5     |
  |--------------------------+-------------+-------------+------------|
  | Turbo Pascal (optimized) |   4.6       |    5.0      |    3.1     |
  |--------------------------+-------------+-------------+------------|
  | Free Pascal              |   7.8       |    4.1      |    3.8     |
  |--------------------------+-------------+-------------+------------|
  | Free Pascal (optimized)  |   4.8       |    2.6      |    2.5     |
  |--------------------------+-------------+-------------+------------|
  | Modula-2                 |   3.3       |    4.8      |    3.8     |
  |--------------------------+-------------+-------------+------------|
  | Modula-2  (optimized)    |   1.6       |    4.2      |    2.9     |
  |--------------------------+-------------+-------------+------------|
  | C#                       |   2.2       |    2.0      |    2.2     |
  |--------------------------+-------------+-------------+------------|
  | Java                     |   4.7       |    2.8      |    3.0     |
  |--------------------------+-------------+-------------+------------|
  | Parva                    |   550       |    210      |    205     |
  |--------------------------+-------------+-------------+------------|
  | Parva (optimized)        |   382       |    150      |    150     |
  `-------------------------------------------------------------------'

We note several points of interest:

(a) When the run-time checks are disabled the Turbo Pascal compiler performs spectacularly better - if one likes to live dangerously this is the way to go.

(b) The Queens1 programs using global variables are rather better than the Queens ones using parameters for all except C# and Java, where, curiously, the opposite behaviour was noted.

(c) The Parva interpreted system is about two orders of magnitude slower than the native-code systems. Even here we can see the benefits of using an optimizing system, simple though this is (later we shall discuss this in more detail).

(d) In deriving these figures some experimentation was first needed so as to find parameters that would yield times sufficiently long to make comparisons meaningful. The times were obtained by simple use of a stopwatch, and of course there is always an element of reaction time in such measurements. The output statements were commented out so that all that was really being measured was the time for the algorithms themselves.

(e) The C# system seems consistently better than the Java system. Both of these employ a JIT (just in time) phase in compilation, rather than being interpreted.


Task 11 - The hailstone sequence

A hailstone sequence is easily generated, save for the annoyance of not having the % (modulo) operator and an else for the if statement. Almost unbelievably, some groups handed in solution using % and else. Did their authors really think I wouldn't notice?

One simple solution follows

  /* Solve the hailstone series problem
     P.D. Terry, Rhodes University, 2003 */

  int lengthOfSeries(int term) {
  // return the length of the series that starts at term
    int n = 1, next;
    while (term != 1) {
      n = n + 1;
      if (term / 2 * 2 != term) next = 3 * term + 1;
      if (term / 2 * 2 == term) next = term / 2;
      term = next;
    }
    return n;
  }

  void main () {
  // Finds the smallest initial term so that we get a run of more than limit
  // terms in the hailstone series
    int limit;
    read("What is the length to exceed?", limit);
    int start = 0, length = 0;
    while (length < limit) {
      start = start + 1;
      length = lengthOfSeries(start);
    }
    write("Smallest initial number was ", start, " leading to ", length, " terms");
  }

Here is part of another solution, which uses the property of the return statement to compensate for the lack of an else:

  int nextTerm(int term) {
  // return next term in a hailstone sequence
    if (term / 2 * 2 != term) return 3 * term + 1;
    return term / 2;
  }

  int lengthOfSeries(int term) {
  // return the length of the series that starts at term
    int n = 1;
    while (term != 1) {
      n = n + 1;
      term = nextTerm(term);
    }
    return n;
  }

Some people thought recursively (perhaps a little less neatly than this, however):

  int lengthOfSeries(int term) {
  // return the length of the series that starts at term
    if (term == 1) return 1;
    if (term / 2 * 2 != term) return 1 + lengthOfSeries(3 * term + 1);
    return 1 + lengthOfSeries(term / 2);
  }

A recursive solution would be considerably less efficient than an iterative one, as it would involve the overhead of multiple function calls and parameter passing.


Task 12 - Lucky numbers

Some very complicated solutions were received. It is possible to solve this problem in several ways. One way is to move numbers from the higher parts of the list so as to let them overwrite numbers in the lower part of the list that are no longer relevant. A solution that exploits this idea follows:

  // Simple test bed program for checking on the generation of lucky numbers
  // P.D. Terry, Rhodes University, 2003

  int GenerateLuckyNumbers(int[] list, int max) {
  // Generate a list of the lucky numbers between 1 and max (inclusive)
  // and return the length of the sequence
    int length = max, i = 0;
    while (i < length) {
      list[i] = i + 1;
      i = i + 1;
    }
    int step = 2;
    while (step <= length) {
      int copied = 0;
      i = 1;
      while (i <= length) {
        if (i - (i / step) * step != 0) {
          list[copied] = list[i-1];
          copied = copied + 1;
        }
        i = i + 1;
      }
      length = copied;
      step = step + 1;
    }
    return length;
  }

  void main() {
    int limit;
    read("Supply limit of numbers to be tested for luck ", limit);
    int[] luckyList = new int[limit];
    int n = GenerateLuckyNumbers(luckyList, limit);
    int i = 0;
    while (i < n) {
      write(luckyList[i], "\t");
      i = i + 1;
    }

Another solution that I received intrigued me, as at first I thought it must be incorrect. On further investigation it turned out to be correct, but a little clumsy, in that it made far more passes over the list than were needed. A little thought showed that it could be improved quite easily. It's always fun to be shown things one had not thought of earlier, so for the record, here is my version of this submission. This algorithm does not move the numbers around in the array, and so has the disadvantage that one has to keep scanning over the whole of the long array on each pass; in the solution above, the list rather rapidly gets shorter.

  // Another simple program for generating a list of lucky numbers
  // P.D. Terry, Rhodes University, 2003
  // Based on an idea by P. Heideman, D. Terry and M. Whitfield, 2003

  int mod(int x, int n) {
    return x - (x/n)*n;
  }

  void main () {
    int limit;
    read("Supply limit of numbers to be tested for luck ", limit);

    // generate an initial sieve
    bool[] lucky = new bool[limit];
    int i = 0;
    while (i < limit) {
      lucky[i] = true;
      i = i + 1;
    }

    // perform passes over the sieve
    int step = 2;
    int remaining = limit;
    while (step <= remaining) {
      i = 0;
      remaining = 1;
      while (i < limit) {
        if ((mod(remaining, step) == 0) && lucky[i]) {
          lucky[i] = false;
          remaining = remaining + 1;
        }
        if (lucky[i]) remaining = remaining + 1;
        i = i + 1;
      }
      step = step + 1;
    }

    // display the surviving lucky numbers
    i = 0;
    while (i < limit) {
      if (lucky[i]) write(i + 1);
      i = i + 1;
    }
  }


Task 13 Timetable Clashes

Quite a number of people ran out of steam and did not attempt this one, and quite a number of the submissions were very badly coded, with errors of style such as including multiple copies of the same code "cut and pasted" into place, or attempting to reread the entire file multiple times. Here is my suggested solution, which you are encouraged to study, if only to see how to use the I/O routines and the set handling routines that were the real motivation for setting this problem in the first place in preparation for future practicals:

  // Rhodes University Lecture clash checking program
  // P.D. Terry, Rhodes University, 2003

  import java.util.*;
  import Library.*;

  class Subjects {
    public String name;
    public SymSet periods = new SymSet();
  }

  class Clash {

    static Subjects[] offered = new Subjects[2000];

    static int findSubject(int s, int n) {
    // s is a prompt number (1 or 2), n is the number of subjects on the timetable
    // Prompts for a subject mnemonic and then returns the index of that subject in
    // the timetable record list
      int i;
      do {
        IO.write("\nSubject " + s + " (or STOP) ? ");
        String name = IO.readLine().trim().toUpperCase();  // clean up
        if (name.equals("STOP")) System.exit(0);
        offered[0].name = name;  // sentinel
        i = n;                   // simple linear search from top end
        while (!name.equals(offered[i].name)) i--;
      } while (i == 0);          // force them to supply a valid mnemonic
      return i;
    }

    static void listPeriods(SymSet periods) {
    // Lists times of lectures that are members of set periods
      for (int hex = 17; hex <= 90; hex++) {
        if (periods.contains(hex)) {
          switch (hex / 16) {
            case 1: IO.write(" Mon"); break;
            case 2: IO.write(" Tue"); break;
            case 3: IO.write(" Wed"); break;
            case 4: IO.write(" Thu"); break;
            case 5: IO.write(" Fri"); break;
            case 6: IO.write(" Sat"); break;
            case 7: IO.write(" Sun"); break;
            default: IO.write(" Bad data"); System.exit(1); break;
          }
          IO.write(hex % 16);
        }
      }
      IO.writeLine();
    }

    static void reportSubject(int i) {
    // Reports name and periods for subject i
      IO.write("\n" + offered[i].name + "    ");
      listPeriods(offered[i].periods);
    }

    static int readTimeTable() {
    // Reads timetable records and returns the number of subjects found
      final int
        mnemonic = 7,
        filler = 42;
      InFile data = new InFile("timetabl");
      if (data.openError()) {
        IO.write("timetable file could not be found");
        System.exit(1);
      }
      data.readln();                               // ignore date line
      int n = 0;                                   // no subjects yet
      offered[0] = new Subjects();                 // prepare for later sentinel
      do {
        char ch = data.readChar();
        if (ch != ';') {                           // check for comment lines
          n++;                                     // one more subject
          offered[n] = new Subjects();
          ch = data.readChar();                    // skip column 2
          offered[n].name = data.readString(mnemonic).toUpperCase().trim();
          String skip = data.readString(filler);   // skip unwanted part
          String s = data.readWord();              // first period data
          while (!s.equals(".")) {                 // lines end with a period
            if (Character.isDigit(s.charAt(0))) {  // ignore alternative periods
              int hex = Integer.parseInt(s, 16);   // base 16 is useful here!
              offered[n].periods.incl(hex);        // add to set of fixed periods
            }
            s = data.readWord();                   // next period data
          }
        }
        data.readln();                             // ready for next subject
      } while (!data.eof());
      return n;                                    // number of subjects found
    }

    public static void main(String[] args) {
      int n = readTimeTable();
      while (true) {                               // loop will terminate when STOP is read
        int first = findSubject(1, n);
        int second = findSubject(2, n);
        if (first != second) {                     // silly case!
          reportSubject(first);
          reportSubject(second);
          SymSet common = offered[first].periods.intersection(offered[second].periods);
          IO.write("\n" + common.members() + " clashes");
          if (common.members() != 0) listPeriods(common);
          IO.writeLine();
        }
      }
    }

  }

The problem is very easily solved using sets. The slightly unusual use of a hexadecimal interpretation of the period data allows for subjects in period 10 to be handled in the same way as at any other time.

Several people did not notice that the comment lines beginning with a ; could appear anywhere within the data file, and not merely at the beginning. Several others wrote a lot of exception handling code for dealing with I/O - but the whole point of developing an I/O library is to keep all that stuff hidden in the closet and not obtruding into one's programs.

Note the simple linear search used in findSubject, which makes use of a so-called sentinel in the zeroth position of the array to keep the search loop simple. You will have been told that linear searches are inefficient, and so they are, but for a simple application like this they are quite adequate, and we shall use them again in the weeks ahead.

A point that was missed by some people is that the strings should be trimmed and turned into some consistent case before making the comparisons.


Home  © P.D. Terry