#
Chapter 5

Ex 5.1
  `REF INT`
Ex 5.2
  `REAL`
Ex 5.3
  The right-hand side of the identity declaration should yield a value of mode `INT`. Insert `ENTIER` or `ROUND` before the `r`.

Ex 5.4
  No value has been assigned to `x` when the second assignment is elaborated.
Ex 5.5

  (a)
    A name with mode `REF REAL`.
  (b)
    The real number denoted by `2.5` with mode `REAL`.
Ex 5.6
  `1.166666`...

Ex 5.7
  A name with mode `REF[,]REAL`.
Ex 5.8

  (a)
    The bounds of the slice on the left-hand side of the assignment are `[-2:0]`, but the bounds of `n` are `[1:3]`. The assignment will cause a run-time error.
  (b)
    You could write `m[5,]:=m[,-1]`, but it is unlikely that you would get what you wanted because the second column overlaps the third row. Here is a solution guaranteed to work:-

           []INT temp = m[,-1];
           m[5,]:=temp[@-2]

Ex 5.9
  There is no known formula which will tell you how big the sieve must be to find the 365th prime; you just have to guess. A sieve with `size` equal to 5000 suffices. You need a counter for the primes. The complete program is:-

         PROGRAM ex5 9 CONTEXT VOID
         USE standard
         BEGIN
            INT size=5000;
            REF[]BOOL flags = LOC[2:size]BOOL;

            FOR i FROM LWB flags TO UPB flags
            DO
               flags[i]:=TRUE
            OD;

            FOR i FROM LWB flags TO UPB flags
            DO
               IF flags[i]
               THEN
                  FOR k FROM i*2 BY --i TO UPB flags
                  DO
                     flags[k]:=FALSE
                     CO Remove multiples of i CO
                  OD
               FI
            OD;

            REF INT count = LOC INT:=0;

            FOR i FROM LWB flags TO UPB flags
            DO
               IF flags[i] ANDTH (count+:=1)=365
               THEN print(i)
               FI
            OD
         END
         FINISH

Ex 5.10

  (a)
    A name of mode `REF FLEX[]CHAR`.
  (b)
    1 and 5.

Ex 5.11

         PROGRAM ex5 11 CONTEXT VOID
         USE standard
         BEGIN
            REF STRING ss = LOC STRING;
            FOR c FROM ABS "a" TO ABS "z"
            DO
               ss:="a"-REPR c;  print((ss,newline))
            OD
         END
         FINISH

Ex 5.12

         REF FLEX[,]REAL f=
            LOC FLEX[1:0,1:0]REAL;
         f:=(5.0,10.0,15.0,20.0);
         print((1 LWB f,1 UPB f,
                2 LWB f,2 UPB f))

Ex 5.13

         PROGRAM ex5 13 CONTEXT VOID
         USE standard
         BEGIN
            REF REAL a = LOC REAL,
                  b = LOC REAL;

            print(Enter two real numbers->");
            read((a,b,newline));
            print(("Their sum is",a+b,newline,
                 "Their product is",a*b))
         END
         FINISH

Ex 5.14

         PROGRAM ex5 14 CONTEXT VOID
         USE standard
         BEGIN
            REF STRING line = LOC STRING;

            DO
              read((line,newline));
              IF UPB line = 0
              THEN stop #terminate the program#
              ELSE
                FOR i
                FROM UPB line BY -1 TO LWB line
                DO
                  print(line[i])
                OD;
                print(newline)
              FI
            OD
         END
         FINISH

Ex 5.15

      REF[]REAL r=
         LOC[(REF INT s=LOC INT; read(s); s)]REAL

Ex 5.16

         PROGRAM ex5 16 CONTEXT VOID
         USE standard
         BEGIN
            REF INT number=LOC INT;
            read(number);

            REF[]INT multiple=LOC[number]INT;
            read(multiple);
            REF INT sum=LOC INT:=0;

            FOR i TO number
            DO
              sum+:=multiple[i]
            OD;
            print(sum)
         END
         FINISH

Ex 5.17

         PROGRAM ex5 17 CONTEXT VOID
         USE standard
         BEGIN
            REF INT neg = LOC INT:=0,
                    pos = LOC INT:=0;

            WHILE
               REF INT i=LOC INT;
               read((i.newline));
               i /= 0
            DO
               (i < 0|neg|pos) +:= i
            OD;

            print(("Sum of negative integers =",
                   neg,newline,
                   "Sum of positive integers =",
                   pos,newline))
         END
         FINISH

Ex 5.18

         PROGRAM ex5 18 CONTEXT VOID
         USE standard
         BEGIN
            REF STRING line = LOC STRING;

            WHILE
               read((line,newline));
               UPB line /= 0
            DO
              REF INT v=LOC INT:=0;

              FOR i TO UPB line
              DO
                v+:=ABS line[i]*i
              OD;

              print((line,v,newline))
            OD
         END
         FINISH

Ex 5.19

  (a)
    `[100]CHAR rc`
  (b)
    `FLEX[1:0]INT fi`
  (c)
    `BOOL b:=TRUE`
Ex 5.20

  (a)
    `REF INT a=LOC INT, b=LOC INT, c=LOC INT`
  (b)
    `REF REAL x=LOC REAL;`
    `REF[]CHAR y=LOC[5]CHAR;`
    `REF[,]REAL z=LOC[3,3]REAL`
  (c)
    `REF FLEX[]CHAR s=LOC FLEX[1:0]CHAR`

Ex 5.21
  `REF[]INT m=LOC[1000]INT;` `[1000]INT m`
Ex 5.22

         PROGRAM ex5 22 CONTEXT VOID
         USE standard
         BEGIN
            REAL sum:=0.0, salary, INT num:=0;

            WHILE read(salary);  salary /= -1.00
            DO
              sum+:=salary;  num+:=1
            OD;

            print(("Average salary=",sum/num))
         END
         FINISH

Ex 5.23
  When writing a program as involved as this, do not expect to get it right first time. In practice, a programmer adds fine details to a program after she has designed the main structure.

         PROGRAM ex5 23 CONTEXT VOID
         USE standard
         BEGIN
            BOOL in word:=FALSE,
            STRING line;
            INT line start, line finish;
            INT word start, word finish;

            read((line,newline));
            line start:=LWB line;
            line finish:=UPB line;

            WHILE line[line start]=blank
                         &
                  line start<=UPB line
            DO
              line start+:=1
            OD;

            WHILE line[line finish]=blank
                         &
                  line finish>=line start
            DO
              line finish-:=1
            OD;

            line:=line[line start:line finish]
                             +blank;

            FOR c FROM LWB line
            WHILE c <= UPB line
            DO
              CHAR lc = line[c];

              IF   lc /= blank & NOT in word
              THEN word start:=c;  in word:=TRUE
              ELIF lc = blank & NOT in word
              THEN SKIP
              ELIF lc /= blank & in word
              THEN SKIP
              ELSE #lc = blank & in word#
                word finish:=c-1;
                in word:=FALSE;
                print((line[
                        word start:word finish],
                       newline))
              FI
            OD
         END
         FINISH

  Notice that both `word start` and `word finish` are made to refer to new values before being used. This is a good check that you are writing the program properly. Notice also that the four possible states of the compound condition on line 26 are carefully spelled out on lines 28, 30 and 32.