Subsections

- Exercises

# Writing reports

One of the problems of using the rather primitive facilities given so far for the output of real and integer numbers is that although they allow numbers to be printed in columns, the column widths are fixed. You might not always want 18 decimal places. To print reports where numbers must be neatly tabulated, it is necessary to have some other means of controlling the size of the resulting strings. The procedures `whole`, `fixed` and `float` provide this capability.

The procedure `whole` has the header

       PROC whole = (NUMBER v, INT width)STRING:

and takes two parameters. The first is a real or integer value (modes `REAL` or `INT`)10.4 and the second is an integer which tells `whole` the field width of the output number (the space in your output book required to accommodate a value is often called a field). If `width` is zero, then the number is printed with the minimum possible width and this will be wider for larger numbers. A positive value for `width` will give numbers preceded by a "+" if positive and a "-" if negative and the number output right-justified within the field with spaces before the sign. A negative value for `width` will only provide a minus sign for negative numbers and the width will be `ABS width`.

Try writing a program with the following fragment included:

       []INT ri = (0,99,-99,9 999,99 999);
       []CHAR wh pr = "Parameter for whole is";

       FOR wi FROM -6 BY --3 TO 6
       DO
          print((wh pr,wi,newline));
          FOR i TO UPB ri
          DO
             write((whole(ri[i],wi),newline))
          OD
       OD

Notice that where the integer is wider than the available space, the output field is filled with the character denoted by `error char` (which is declared in the standard prelude as the asterisk (`*`) with mode `CHAR`), so it is wise to ensure that your output field is large enough to accommodate the largest number you might want to print.

If you give a real number to `whole`, it calls the procedure `fixed` with parameters `width` and `0`.

The procedure `fixed` has the header

       PROC fixed = (NUMBER v,
                     INT width, after)STRING:

and takes three parameters. The first two are the same as those for `whole` and the third specifies the number of decimal places required. The rules for `width` are the same as the rules for `width` for `whole`.

When you want to print numbers in scientific format (that is, with an exponent), you should use `float` which takes four parameters and has the header

        PROC float = (NUMBER v,
                      INT width, after, exp)STRING:

The first three are the same as the parameters for `fixed`, while the fourth is the width of the exponent field. The version of `float` supplied in the transput library uses `e` to separate the exponent from the rest of the number. Thus the call

       print(("[",float(pi*10,8,2,-2),"]"))

produces the output `[+3.14e 1]`. The parameter `exp` obeys the same rules as `width`.

Note that the transput of data in Algol 68 is so organised that values output by an Algol 68 program can be input by another (or the same) program.

Here are some exercises which test you on your understanding of `whole`, `fixed` and `float`.

## Exercises

9.13
  The monthly rainfall for a particular location is given by the following figures:

          6.54  12.3   10.1   13.83  5.04  9.15
         14.34  16.38  13.84  10.45  8.49  7.57

  Write a program which will print the figures vertically, each preceded by the name of the month. The months and the figures should line up vertically, the months left-justified, the figures with decimal points aligned. Ans
9.14
  Write a program which will print the square roots of all the integers from 1 to 100 to 4 decimal places. Each number should be preceded by the corresponding integer. So, for example, the program should print `2 1.4142` as a column-pair. Print the whole table in four columns with 25 entries in each column, the numbers 1-25 being in the first column. Ans
9.15
  Write a program which will list the terms in the series , , , ..., . Each value should be written in scientific notation with 6 decimal places, and should be preceded by the value of the power (i.e., the numbers 1 to 10). Use a field width of 12. Ans