Home » Personal collection » Acorn hard disk » files » DomFord » INet/samples

INet/samples

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Personal collection » Acorn hard disk » files » DomFord
Filename: INet/samples
Read OK:
File size: 1C57 bytes
Load address: 0000
Exec address: 0000
File contents
program Samples;
{ Borland Pascal 7.

  The sample problems for the 1995 British Informatics Olympiad.
  (C) Antony Rix 1995. 
  This file/program/printout may only be used with the BIO }

{-------------------- The Problems -----------------------} {

Problem 1 - Prime Numbers
=========================

A prime number is defined as any integer greater than one which
contains no factors other than one and itself - that is, if there
is no number between one and N which can divide N with no remainder,
then N is prime.

Your program should ask for a maximum number, M, and search
for and print all prime numbers in the range 2 to M.  You should
also display the number of prime numbers found.  M will not
be greater than 2000.

Sample Output for Problem 1
===========================

Maximum number to test? 21
The following numbers between 2 and 21 are prime:
2 3 5 7 11 13 17 19
A total of 8 numbers were found in this range.

Problem 2 - Word Count
======================

You should write a program which asks for the name of a file
and then counts the number of words in the file.  A word is
defined as any sequence of symbols separated by any combination of
space characters and/or line breaks.

The file will not contain lines more than 127
characters long and will have a maximum of 20000 words.

(Take care when creating a test file to make sure it ends
with at least one blank line, otherwise it may be ignored
by your programming language.)

Sample file for Problem 2 (sample.fil)
======================================

The cat sat on the mat.
The cow jumped over the moon.
She sells sea shells on the sea shore.

Sample Output for Problem 2
===========================

Name of file for word count? sample.fil
The file sample.fil contains 20 words.


Problem 3 - Bubble sort
=======================

To solve this problem you should write a program to sort
numbers.  It should read in integers from the keyboard,
ending with and ignoring the number -999, sort them and
print them in order of smallest first.  There will be
between 2 and 20 numbers to be sorted.

You may use a simple sorting method called bubblesort.
This simply compares each number with its next neighbour
in turn, swapping them if they are different, and
repeatedly goes through the file until no more changes
can be made.

Sample Output for Problem 3
===========================

Type in up to 20 numbers, ending with -999:
23
12
45
32
8
-1
2
-999
The 7 sorted numbers are:
-1
2
8
12
23
32
45

}
{-------------------- The Solution -----------------------}

procedure Primes;
{ Solves problem 1: find prime numbers from 2 to m. }

  function IsPrime(N: Integer): Boolean;
  { returns True if N is prime.
    This is done by trying to find an integer j between 2
    and the square root of N that divides N exactly;
    N is prime if no such number exists. }
  var
    j, Stop: Integer;
    Factored: Boolean;
  begin
    Factored := False;
    j := 2;
    Stop := Trunc(Sqrt(N)) + 1; { stop at the square root of N }

    { test for factors of N and stop of one is found. }
    while (j <= Stop) and (j < N) and not Factored do
    begin
      Factored := ((N mod j) = 0);
      j := j + 1;
    end;
    IsPrime := not Factored;
  end;

var M, N, I: Integer;
begin
  WriteLn('Find prime numbers.');
  WriteLn;
  { N = total number of primes found }
  N := 0;
  Write('Maximum number to test? ');
  ReadLn(M);
  WriteLn('The following numbers between 2 and ', M:1, ' are prime:');
  { test the numbers 2 to M }
  for i := 2 to M do if IsPrime(I) then
  begin
    N := N + 1;
    Write(i:1, ' ');
  end;
  WriteLn;
  WriteLn('A total of ', N:1, ' numbers were found in this range.');
end;

procedure WordCount;
{ a solution of problem 2: count words in a file. }
{ This is done by reading in characters from the file
  one by one, testing if each character is a space/CR
  or not, and incrementing the word count whenever it passes
  from space to a word. }
var
  F: Text;
  Name: String;
  c: Char;
  Count: Integer;
  InWord, WasInWord: Boolean;
begin
  WriteLn('Count Words');
  WriteLn;
  Write('Name of file for word count? ');
  ReadLn(Name);
  { open the file for reading }
  Assign(F, Name);
  Reset(F);
  Count := 0;
  InWord := False;
  while not EOF(F) do
  begin
    WasInWord := InWord;
    { end of line counts as a word break }
    if EOLn(F) then
    begin
      ReadLn(F);
      InWord := False;
    end
    else
    begin
      Read(F, c);
      InWord := (c <> ' ');
      { not in a word if c is space }
    end;
    if (not WasInWord) and InWord then
    { passed from space into a word }
      Count := Count + 1;
  end;
  Close(F);
  WriteLn('The file ', Name, ' contains ', Count:1, ' words.');
end;

procedure BubbleSort;
{ problem 3: read in and bubble sort numbers. }
const Max = 20;
type List = array[1..Max] of Integer;

  procedure Swap(var A, B: Integer);
  var C: Integer;
  begin
    C := A;
    A := B;
    B := C;
  end;

  procedure Sort(var Data: List; n: Integer);
  var
    i, j: Integer;
    HasChanged: Boolean;
  begin
    { bubblesort works by comparing adjacent values of the
      data and swapping them if necessary. }
    i := 1;
    HasChanged := True;
    while (i < n) and HasChanged do
    begin
      HasChanged := False;
      for j := n - 1 downto i do
        if Data[j + 1] < Data[j] then
        begin
          Swap(Data[j + 1], Data[j]);
          HasChanged := True;
        end;
      i := i + 1;
    end;
  end;

var
  n, x: Integer;
  Data: List;
begin
  WriteLn('Bubblesort of numbers');
  WriteLn;
  WriteLn('Type in up to 20 numbers, ending with -999:');
  n := 0;
  repeat
    ReadLn(x);
    if (x <> -999) and (n < 20) then
    begin
      n := n + 1;
      Data[n] := x;
    end;
  until x = -999;
  Sort(Data, n);
  WriteLn('The ', n:1, ' sorted numbers are:');
  for x := 1 to n do
  WriteLn(Data[x]);
end;

procedure Menu;
{ the program's main menu, allowing choice of problems to test. }
var o: Integer;
begin
  repeat
    WriteLn('Choose a menu option:');
    WriteLn;
    WriteLn('1) Problem 1: Find prime numbers.');
    WriteLn('2) Problem 2: Word Count.');
    WriteLn('3) Problem 3: Bubblesort.');
    WriteLn('4) Exit the program.');
    WriteLn;
    Write('Choice? ');
    ReadLn(o);
    case o of
      1: Primes;
      2: WordCount;
      3: BubbleSort;
      4: WriteLn('Exiting...');
    else
      WriteLn(o, ' is not a valid menu option.');
    end;
    WriteLn;
    if o <> 4 then
    begin
      Write('Press <enter> to continue...');
      ReadLn;
    end;
    WriteLn;
  until o = 4;
end;

begin
  { main program }
  WriteLn;
  WriteLn('*******************************************************');
  WriteLn('*                                                     *');
  WriteLn('*  British Informatics Olympiad 1995                  *');
  WriteLn('*  =================================                  *');
  WriteLn('*                                                     *');
  WriteLn('*  Sample problems        -      (C) Antony Rix 1995  *');
  WriteLn('*  Question statement and example solution in Pascal  *');
  WriteLn('*                                                     *');
  WriteLn('*******************************************************');
  WriteLn;
  Menu;
end.
00000000  70 72 6f 67 72 61 6d 20  53 61 6d 70 6c 65 73 3b  |program Samples;|
00000010  0a 7b 20 42 6f 72 6c 61  6e 64 20 50 61 73 63 61  |.{ Borland Pasca|
00000020  6c 20 37 2e 0a 0a 20 20  54 68 65 20 73 61 6d 70  |l 7...  The samp|
00000030  6c 65 20 70 72 6f 62 6c  65 6d 73 20 66 6f 72 20  |le problems for |
00000040  74 68 65 20 31 39 39 35  20 42 72 69 74 69 73 68  |the 1995 British|
00000050  20 49 6e 66 6f 72 6d 61  74 69 63 73 20 4f 6c 79  | Informatics Oly|
00000060  6d 70 69 61 64 2e 0a 20  20 28 43 29 20 41 6e 74  |mpiad..  (C) Ant|
00000070  6f 6e 79 20 52 69 78 20  31 39 39 35 2e 20 0a 20  |ony Rix 1995. . |
00000080  20 54 68 69 73 20 66 69  6c 65 2f 70 72 6f 67 72  | This file/progr|
00000090  61 6d 2f 70 72 69 6e 74  6f 75 74 20 6d 61 79 20  |am/printout may |
000000a0  6f 6e 6c 79 20 62 65 20  75 73 65 64 20 77 69 74  |only be used wit|
000000b0  68 20 74 68 65 20 42 49  4f 20 7d 0a 0a 7b 2d 2d  |h the BIO }..{--|
000000c0  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
000000d0  2d 2d 20 54 68 65 20 50  72 6f 62 6c 65 6d 73 20  |-- The Problems |
000000e0  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |----------------|
000000f0  2d 2d 2d 2d 2d 2d 2d 7d  20 7b 0a 0a 50 72 6f 62  |-------} {..Prob|
00000100  6c 65 6d 20 31 20 2d 20  50 72 69 6d 65 20 4e 75  |lem 1 - Prime Nu|
00000110  6d 62 65 72 73 0a 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |mbers.==========|
00000120  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 0a  |===============.|
00000130  0a 41 20 70 72 69 6d 65  20 6e 75 6d 62 65 72 20  |.A prime number |
00000140  69 73 20 64 65 66 69 6e  65 64 20 61 73 20 61 6e  |is defined as an|
00000150  79 20 69 6e 74 65 67 65  72 20 67 72 65 61 74 65  |y integer greate|
00000160  72 20 74 68 61 6e 20 6f  6e 65 20 77 68 69 63 68  |r than one which|
00000170  0a 63 6f 6e 74 61 69 6e  73 20 6e 6f 20 66 61 63  |.contains no fac|
00000180  74 6f 72 73 20 6f 74 68  65 72 20 74 68 61 6e 20  |tors other than |
00000190  6f 6e 65 20 61 6e 64 20  69 74 73 65 6c 66 20 2d  |one and itself -|
000001a0  20 74 68 61 74 20 69 73  2c 20 69 66 20 74 68 65  | that is, if the|
000001b0  72 65 0a 69 73 20 6e 6f  20 6e 75 6d 62 65 72 20  |re.is no number |
000001c0  62 65 74 77 65 65 6e 20  6f 6e 65 20 61 6e 64 20  |between one and |
000001d0  4e 20 77 68 69 63 68 20  63 61 6e 20 64 69 76 69  |N which can divi|
000001e0  64 65 20 4e 20 77 69 74  68 20 6e 6f 20 72 65 6d  |de N with no rem|
000001f0  61 69 6e 64 65 72 2c 0a  74 68 65 6e 20 4e 20 69  |ainder,.then N i|
00000200  73 20 70 72 69 6d 65 2e  0a 0a 59 6f 75 72 20 70  |s prime...Your p|
00000210  72 6f 67 72 61 6d 20 73  68 6f 75 6c 64 20 61 73  |rogram should as|
00000220  6b 20 66 6f 72 20 61 20  6d 61 78 69 6d 75 6d 20  |k for a maximum |
00000230  6e 75 6d 62 65 72 2c 20  4d 2c 20 61 6e 64 20 73  |number, M, and s|
00000240  65 61 72 63 68 0a 66 6f  72 20 61 6e 64 20 70 72  |earch.for and pr|
00000250  69 6e 74 20 61 6c 6c 20  70 72 69 6d 65 20 6e 75  |int all prime nu|
00000260  6d 62 65 72 73 20 69 6e  20 74 68 65 20 72 61 6e  |mbers in the ran|
00000270  67 65 20 32 20 74 6f 20  4d 2e 20 20 59 6f 75 20  |ge 2 to M.  You |
00000280  73 68 6f 75 6c 64 0a 61  6c 73 6f 20 64 69 73 70  |should.also disp|
00000290  6c 61 79 20 74 68 65 20  6e 75 6d 62 65 72 20 6f  |lay the number o|
000002a0  66 20 70 72 69 6d 65 20  6e 75 6d 62 65 72 73 20  |f prime numbers |
000002b0  66 6f 75 6e 64 2e 20 20  4d 20 77 69 6c 6c 20 6e  |found.  M will n|
000002c0  6f 74 0a 62 65 20 67 72  65 61 74 65 72 20 74 68  |ot.be greater th|
000002d0  61 6e 20 32 30 30 30 2e  0a 0a 53 61 6d 70 6c 65  |an 2000...Sample|
000002e0  20 4f 75 74 70 75 74 20  66 6f 72 20 50 72 6f 62  | Output for Prob|
000002f0  6c 65 6d 20 31 0a 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |lem 1.==========|
00000300  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |================|
00000310  3d 0a 0a 4d 61 78 69 6d  75 6d 20 6e 75 6d 62 65  |=..Maximum numbe|
00000320  72 20 74 6f 20 74 65 73  74 3f 20 32 31 0a 54 68  |r to test? 21.Th|
00000330  65 20 66 6f 6c 6c 6f 77  69 6e 67 20 6e 75 6d 62  |e following numb|
00000340  65 72 73 20 62 65 74 77  65 65 6e 20 32 20 61 6e  |ers between 2 an|
00000350  64 20 32 31 20 61 72 65  20 70 72 69 6d 65 3a 0a  |d 21 are prime:.|
00000360  32 20 33 20 35 20 37 20  31 31 20 31 33 20 31 37  |2 3 5 7 11 13 17|
00000370  20 31 39 0a 41 20 74 6f  74 61 6c 20 6f 66 20 38  | 19.A total of 8|
00000380  20 6e 75 6d 62 65 72 73  20 77 65 72 65 20 66 6f  | numbers were fo|
00000390  75 6e 64 20 69 6e 20 74  68 69 73 20 72 61 6e 67  |und in this rang|
000003a0  65 2e 0a 0a 50 72 6f 62  6c 65 6d 20 32 20 2d 20  |e...Problem 2 - |
000003b0  57 6f 72 64 20 43 6f 75  6e 74 0a 3d 3d 3d 3d 3d  |Word Count.=====|
000003c0  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |================|
000003d0  3d 0a 0a 59 6f 75 20 73  68 6f 75 6c 64 20 77 72  |=..You should wr|
000003e0  69 74 65 20 61 20 70 72  6f 67 72 61 6d 20 77 68  |ite a program wh|
000003f0  69 63 68 20 61 73 6b 73  20 66 6f 72 20 74 68 65  |ich asks for the|
00000400  20 6e 61 6d 65 20 6f 66  20 61 20 66 69 6c 65 0a  | name of a file.|
00000410  61 6e 64 20 74 68 65 6e  20 63 6f 75 6e 74 73 20  |and then counts |
00000420  74 68 65 20 6e 75 6d 62  65 72 20 6f 66 20 77 6f  |the number of wo|
00000430  72 64 73 20 69 6e 20 74  68 65 20 66 69 6c 65 2e  |rds in the file.|
00000440  20 20 41 20 77 6f 72 64  20 69 73 0a 64 65 66 69  |  A word is.defi|
00000450  6e 65 64 20 61 73 20 61  6e 79 20 73 65 71 75 65  |ned as any seque|
00000460  6e 63 65 20 6f 66 20 73  79 6d 62 6f 6c 73 20 73  |nce of symbols s|
00000470  65 70 61 72 61 74 65 64  20 62 79 20 61 6e 79 20  |eparated by any |
00000480  63 6f 6d 62 69 6e 61 74  69 6f 6e 20 6f 66 0a 73  |combination of.s|
00000490  70 61 63 65 20 63 68 61  72 61 63 74 65 72 73 20  |pace characters |
000004a0  61 6e 64 2f 6f 72 20 6c  69 6e 65 20 62 72 65 61  |and/or line brea|
000004b0  6b 73 2e 0a 0a 54 68 65  20 66 69 6c 65 20 77 69  |ks...The file wi|
000004c0  6c 6c 20 6e 6f 74 20 63  6f 6e 74 61 69 6e 20 6c  |ll not contain l|
000004d0  69 6e 65 73 20 6d 6f 72  65 20 74 68 61 6e 20 31  |ines more than 1|
000004e0  32 37 0a 63 68 61 72 61  63 74 65 72 73 20 6c 6f  |27.characters lo|
000004f0  6e 67 20 61 6e 64 20 77  69 6c 6c 20 68 61 76 65  |ng and will have|
00000500  20 61 20 6d 61 78 69 6d  75 6d 20 6f 66 20 32 30  | a maximum of 20|
00000510  30 30 30 20 77 6f 72 64  73 2e 0a 0a 28 54 61 6b  |000 words...(Tak|
00000520  65 20 63 61 72 65 20 77  68 65 6e 20 63 72 65 61  |e care when crea|
00000530  74 69 6e 67 20 61 20 74  65 73 74 20 66 69 6c 65  |ting a test file|
00000540  20 74 6f 20 6d 61 6b 65  20 73 75 72 65 20 69 74  | to make sure it|
00000550  20 65 6e 64 73 0a 77 69  74 68 20 61 74 20 6c 65  | ends.with at le|
00000560  61 73 74 20 6f 6e 65 20  62 6c 61 6e 6b 20 6c 69  |ast one blank li|
00000570  6e 65 2c 20 6f 74 68 65  72 77 69 73 65 20 69 74  |ne, otherwise it|
00000580  20 6d 61 79 20 62 65 20  69 67 6e 6f 72 65 64 0a  | may be ignored.|
00000590  62 79 20 79 6f 75 72 20  70 72 6f 67 72 61 6d 6d  |by your programm|
000005a0  69 6e 67 20 6c 61 6e 67  75 61 67 65 2e 29 0a 0a  |ing language.)..|
000005b0  53 61 6d 70 6c 65 20 66  69 6c 65 20 66 6f 72 20  |Sample file for |
000005c0  50 72 6f 62 6c 65 6d 20  32 20 28 73 61 6d 70 6c  |Problem 2 (sampl|
000005d0  65 2e 66 69 6c 29 0a 3d  3d 3d 3d 3d 3d 3d 3d 3d  |e.fil).=========|
000005e0  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |================|
000005f0  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 0a 0a 54  |=============..T|
00000600  68 65 20 63 61 74 20 73  61 74 20 6f 6e 20 74 68  |he cat sat on th|
00000610  65 20 6d 61 74 2e 0a 54  68 65 20 63 6f 77 20 6a  |e mat..The cow j|
00000620  75 6d 70 65 64 20 6f 76  65 72 20 74 68 65 20 6d  |umped over the m|
00000630  6f 6f 6e 2e 0a 53 68 65  20 73 65 6c 6c 73 20 73  |oon..She sells s|
00000640  65 61 20 73 68 65 6c 6c  73 20 6f 6e 20 74 68 65  |ea shells on the|
00000650  20 73 65 61 20 73 68 6f  72 65 2e 0a 0a 53 61 6d  | sea shore...Sam|
00000660  70 6c 65 20 4f 75 74 70  75 74 20 66 6f 72 20 50  |ple Output for P|
00000670  72 6f 62 6c 65 6d 20 32  0a 3d 3d 3d 3d 3d 3d 3d  |roblem 2.=======|
00000680  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |================|
00000690  3d 3d 3d 3d 0a 0a 4e 61  6d 65 20 6f 66 20 66 69  |====..Name of fi|
000006a0  6c 65 20 66 6f 72 20 77  6f 72 64 20 63 6f 75 6e  |le for word coun|
000006b0  74 3f 20 73 61 6d 70 6c  65 2e 66 69 6c 0a 54 68  |t? sample.fil.Th|
000006c0  65 20 66 69 6c 65 20 73  61 6d 70 6c 65 2e 66 69  |e file sample.fi|
000006d0  6c 20 63 6f 6e 74 61 69  6e 73 20 32 30 20 77 6f  |l contains 20 wo|
000006e0  72 64 73 2e 0a 0a 0a 50  72 6f 62 6c 65 6d 20 33  |rds....Problem 3|
000006f0  20 2d 20 42 75 62 62 6c  65 20 73 6f 72 74 0a 3d  | - Bubble sort.=|
00000700  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |================|
00000710  3d 3d 3d 3d 3d 3d 0a 0a  54 6f 20 73 6f 6c 76 65  |======..To solve|
00000720  20 74 68 69 73 20 70 72  6f 62 6c 65 6d 20 79 6f  | this problem yo|
00000730  75 20 73 68 6f 75 6c 64  20 77 72 69 74 65 20 61  |u should write a|
00000740  20 70 72 6f 67 72 61 6d  20 74 6f 20 73 6f 72 74  | program to sort|
00000750  0a 6e 75 6d 62 65 72 73  2e 20 20 49 74 20 73 68  |.numbers.  It sh|
00000760  6f 75 6c 64 20 72 65 61  64 20 69 6e 20 69 6e 74  |ould read in int|
00000770  65 67 65 72 73 20 66 72  6f 6d 20 74 68 65 20 6b  |egers from the k|
00000780  65 79 62 6f 61 72 64 2c  0a 65 6e 64 69 6e 67 20  |eyboard,.ending |
00000790  77 69 74 68 20 61 6e 64  20 69 67 6e 6f 72 69 6e  |with and ignorin|
000007a0  67 20 74 68 65 20 6e 75  6d 62 65 72 20 2d 39 39  |g the number -99|
000007b0  39 2c 20 73 6f 72 74 20  74 68 65 6d 20 61 6e 64  |9, sort them and|
000007c0  0a 70 72 69 6e 74 20 74  68 65 6d 20 69 6e 20 6f  |.print them in o|
000007d0  72 64 65 72 20 6f 66 20  73 6d 61 6c 6c 65 73 74  |rder of smallest|
000007e0  20 66 69 72 73 74 2e 20  20 54 68 65 72 65 20 77  | first.  There w|
000007f0  69 6c 6c 20 62 65 0a 62  65 74 77 65 65 6e 20 32  |ill be.between 2|
00000800  20 61 6e 64 20 32 30 20  6e 75 6d 62 65 72 73 20  | and 20 numbers |
00000810  74 6f 20 62 65 20 73 6f  72 74 65 64 2e 0a 0a 59  |to be sorted...Y|
00000820  6f 75 20 6d 61 79 20 75  73 65 20 61 20 73 69 6d  |ou may use a sim|
00000830  70 6c 65 20 73 6f 72 74  69 6e 67 20 6d 65 74 68  |ple sorting meth|
00000840  6f 64 20 63 61 6c 6c 65  64 20 62 75 62 62 6c 65  |od called bubble|
00000850  73 6f 72 74 2e 0a 54 68  69 73 20 73 69 6d 70 6c  |sort..This simpl|
00000860  79 20 63 6f 6d 70 61 72  65 73 20 65 61 63 68 20  |y compares each |
00000870  6e 75 6d 62 65 72 20 77  69 74 68 20 69 74 73 20  |number with its |
00000880  6e 65 78 74 20 6e 65 69  67 68 62 6f 75 72 0a 69  |next neighbour.i|
00000890  6e 20 74 75 72 6e 2c 20  73 77 61 70 70 69 6e 67  |n turn, swapping|
000008a0  20 74 68 65 6d 20 69 66  20 74 68 65 79 20 61 72  | them if they ar|
000008b0  65 20 64 69 66 66 65 72  65 6e 74 2c 20 61 6e 64  |e different, and|
000008c0  0a 72 65 70 65 61 74 65  64 6c 79 20 67 6f 65 73  |.repeatedly goes|
000008d0  20 74 68 72 6f 75 67 68  20 74 68 65 20 66 69 6c  | through the fil|
000008e0  65 20 75 6e 74 69 6c 20  6e 6f 20 6d 6f 72 65 20  |e until no more |
000008f0  63 68 61 6e 67 65 73 0a  63 61 6e 20 62 65 20 6d  |changes.can be m|
00000900  61 64 65 2e 0a 0a 53 61  6d 70 6c 65 20 4f 75 74  |ade...Sample Out|
00000910  70 75 74 20 66 6f 72 20  50 72 6f 62 6c 65 6d 20  |put for Problem |
00000920  33 0a 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |3.==============|
00000930  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 0a 0a 54  |=============..T|
00000940  79 70 65 20 69 6e 20 75  70 20 74 6f 20 32 30 20  |ype in up to 20 |
00000950  6e 75 6d 62 65 72 73 2c  20 65 6e 64 69 6e 67 20  |numbers, ending |
00000960  77 69 74 68 20 2d 39 39  39 3a 0a 32 33 0a 31 32  |with -999:.23.12|
00000970  0a 34 35 0a 33 32 0a 38  0a 2d 31 0a 32 0a 2d 39  |.45.32.8.-1.2.-9|
00000980  39 39 0a 54 68 65 20 37  20 73 6f 72 74 65 64 20  |99.The 7 sorted |
00000990  6e 75 6d 62 65 72 73 20  61 72 65 3a 0a 2d 31 0a  |numbers are:.-1.|
000009a0  32 0a 38 0a 31 32 0a 32  33 0a 33 32 0a 34 35 0a  |2.8.12.23.32.45.|
000009b0  0a 7d 0a 7b 2d 2d 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |.}.{------------|
000009c0  2d 2d 2d 2d 2d 2d 2d 2d  20 54 68 65 20 53 6f 6c  |-------- The Sol|
000009d0  75 74 69 6f 6e 20 2d 2d  2d 2d 2d 2d 2d 2d 2d 2d  |ution ----------|
000009e0  2d 2d 2d 2d 2d 2d 2d 2d  2d 2d 2d 2d 2d 7d 0a 0a  |-------------}..|
000009f0  70 72 6f 63 65 64 75 72  65 20 50 72 69 6d 65 73  |procedure Primes|
00000a00  3b 0a 7b 20 53 6f 6c 76  65 73 20 70 72 6f 62 6c  |;.{ Solves probl|
00000a10  65 6d 20 31 3a 20 66 69  6e 64 20 70 72 69 6d 65  |em 1: find prime|
00000a20  20 6e 75 6d 62 65 72 73  20 66 72 6f 6d 20 32 20  | numbers from 2 |
00000a30  74 6f 20 6d 2e 20 7d 0a  0a 20 20 66 75 6e 63 74  |to m. }..  funct|
00000a40  69 6f 6e 20 49 73 50 72  69 6d 65 28 4e 3a 20 49  |ion IsPrime(N: I|
00000a50  6e 74 65 67 65 72 29 3a  20 42 6f 6f 6c 65 61 6e  |nteger): Boolean|
00000a60  3b 0a 20 20 7b 20 72 65  74 75 72 6e 73 20 54 72  |;.  { returns Tr|
00000a70  75 65 20 69 66 20 4e 20  69 73 20 70 72 69 6d 65  |ue if N is prime|
00000a80  2e 0a 20 20 20 20 54 68  69 73 20 69 73 20 64 6f  |..    This is do|
00000a90  6e 65 20 62 79 20 74 72  79 69 6e 67 20 74 6f 20  |ne by trying to |
00000aa0  66 69 6e 64 20 61 6e 20  69 6e 74 65 67 65 72 20  |find an integer |
00000ab0  6a 20 62 65 74 77 65 65  6e 20 32 0a 20 20 20 20  |j between 2.    |
00000ac0  61 6e 64 20 74 68 65 20  73 71 75 61 72 65 20 72  |and the square r|
00000ad0  6f 6f 74 20 6f 66 20 4e  20 74 68 61 74 20 64 69  |oot of N that di|
00000ae0  76 69 64 65 73 20 4e 20  65 78 61 63 74 6c 79 3b  |vides N exactly;|
00000af0  0a 20 20 20 20 4e 20 69  73 20 70 72 69 6d 65 20  |.    N is prime |
00000b00  69 66 20 6e 6f 20 73 75  63 68 20 6e 75 6d 62 65  |if no such numbe|
00000b10  72 20 65 78 69 73 74 73  2e 20 7d 0a 20 20 76 61  |r exists. }.  va|
00000b20  72 0a 20 20 20 20 6a 2c  20 53 74 6f 70 3a 20 49  |r.    j, Stop: I|
00000b30  6e 74 65 67 65 72 3b 0a  20 20 20 20 46 61 63 74  |nteger;.    Fact|
00000b40  6f 72 65 64 3a 20 42 6f  6f 6c 65 61 6e 3b 0a 20  |ored: Boolean;. |
00000b50  20 62 65 67 69 6e 0a 20  20 20 20 46 61 63 74 6f  | begin.    Facto|
00000b60  72 65 64 20 3a 3d 20 46  61 6c 73 65 3b 0a 20 20  |red := False;.  |
00000b70  20 20 6a 20 3a 3d 20 32  3b 0a 20 20 20 20 53 74  |  j := 2;.    St|
00000b80  6f 70 20 3a 3d 20 54 72  75 6e 63 28 53 71 72 74  |op := Trunc(Sqrt|
00000b90  28 4e 29 29 20 2b 20 31  3b 20 7b 20 73 74 6f 70  |(N)) + 1; { stop|
00000ba0  20 61 74 20 74 68 65 20  73 71 75 61 72 65 20 72  | at the square r|
00000bb0  6f 6f 74 20 6f 66 20 4e  20 7d 0a 0a 20 20 20 20  |oot of N }..    |
00000bc0  7b 20 74 65 73 74 20 66  6f 72 20 66 61 63 74 6f  |{ test for facto|
00000bd0  72 73 20 6f 66 20 4e 20  61 6e 64 20 73 74 6f 70  |rs of N and stop|
00000be0  20 6f 66 20 6f 6e 65 20  69 73 20 66 6f 75 6e 64  | of one is found|
00000bf0  2e 20 7d 0a 20 20 20 20  77 68 69 6c 65 20 28 6a  |. }.    while (j|
00000c00  20 3c 3d 20 53 74 6f 70  29 20 61 6e 64 20 28 6a  | <= Stop) and (j|
00000c10  20 3c 20 4e 29 20 61 6e  64 20 6e 6f 74 20 46 61  | < N) and not Fa|
00000c20  63 74 6f 72 65 64 20 64  6f 0a 20 20 20 20 62 65  |ctored do.    be|
00000c30  67 69 6e 0a 20 20 20 20  20 20 46 61 63 74 6f 72  |gin.      Factor|
00000c40  65 64 20 3a 3d 20 28 28  4e 20 6d 6f 64 20 6a 29  |ed := ((N mod j)|
00000c50  20 3d 20 30 29 3b 0a 20  20 20 20 20 20 6a 20 3a  | = 0);.      j :|
00000c60  3d 20 6a 20 2b 20 31 3b  0a 20 20 20 20 65 6e 64  |= j + 1;.    end|
00000c70  3b 0a 20 20 20 20 49 73  50 72 69 6d 65 20 3a 3d  |;.    IsPrime :=|
00000c80  20 6e 6f 74 20 46 61 63  74 6f 72 65 64 3b 0a 20  | not Factored;. |
00000c90  20 65 6e 64 3b 0a 0a 76  61 72 20 4d 2c 20 4e 2c  | end;..var M, N,|
00000ca0  20 49 3a 20 49 6e 74 65  67 65 72 3b 0a 62 65 67  | I: Integer;.beg|
00000cb0  69 6e 0a 20 20 57 72 69  74 65 4c 6e 28 27 46 69  |in.  WriteLn('Fi|
00000cc0  6e 64 20 70 72 69 6d 65  20 6e 75 6d 62 65 72 73  |nd prime numbers|
00000cd0  2e 27 29 3b 0a 20 20 57  72 69 74 65 4c 6e 3b 0a  |.');.  WriteLn;.|
00000ce0  20 20 7b 20 4e 20 3d 20  74 6f 74 61 6c 20 6e 75  |  { N = total nu|
00000cf0  6d 62 65 72 20 6f 66 20  70 72 69 6d 65 73 20 66  |mber of primes f|
00000d00  6f 75 6e 64 20 7d 0a 20  20 4e 20 3a 3d 20 30 3b  |ound }.  N := 0;|
00000d10  0a 20 20 57 72 69 74 65  28 27 4d 61 78 69 6d 75  |.  Write('Maximu|
00000d20  6d 20 6e 75 6d 62 65 72  20 74 6f 20 74 65 73 74  |m number to test|
00000d30  3f 20 27 29 3b 0a 20 20  52 65 61 64 4c 6e 28 4d  |? ');.  ReadLn(M|
00000d40  29 3b 0a 20 20 57 72 69  74 65 4c 6e 28 27 54 68  |);.  WriteLn('Th|
00000d50  65 20 66 6f 6c 6c 6f 77  69 6e 67 20 6e 75 6d 62  |e following numb|
00000d60  65 72 73 20 62 65 74 77  65 65 6e 20 32 20 61 6e  |ers between 2 an|
00000d70  64 20 27 2c 20 4d 3a 31  2c 20 27 20 61 72 65 20  |d ', M:1, ' are |
00000d80  70 72 69 6d 65 3a 27 29  3b 0a 20 20 7b 20 74 65  |prime:');.  { te|
00000d90  73 74 20 74 68 65 20 6e  75 6d 62 65 72 73 20 32  |st the numbers 2|
00000da0  20 74 6f 20 4d 20 7d 0a  20 20 66 6f 72 20 69 20  | to M }.  for i |
00000db0  3a 3d 20 32 20 74 6f 20  4d 20 64 6f 20 69 66 20  |:= 2 to M do if |
00000dc0  49 73 50 72 69 6d 65 28  49 29 20 74 68 65 6e 0a  |IsPrime(I) then.|
00000dd0  20 20 62 65 67 69 6e 0a  20 20 20 20 4e 20 3a 3d  |  begin.    N :=|
00000de0  20 4e 20 2b 20 31 3b 0a  20 20 20 20 57 72 69 74  | N + 1;.    Writ|
00000df0  65 28 69 3a 31 2c 20 27  20 27 29 3b 0a 20 20 65  |e(i:1, ' ');.  e|
00000e00  6e 64 3b 0a 20 20 57 72  69 74 65 4c 6e 3b 0a 20  |nd;.  WriteLn;. |
00000e10  20 57 72 69 74 65 4c 6e  28 27 41 20 74 6f 74 61  | WriteLn('A tota|
00000e20  6c 20 6f 66 20 27 2c 20  4e 3a 31 2c 20 27 20 6e  |l of ', N:1, ' n|
00000e30  75 6d 62 65 72 73 20 77  65 72 65 20 66 6f 75 6e  |umbers were foun|
00000e40  64 20 69 6e 20 74 68 69  73 20 72 61 6e 67 65 2e  |d in this range.|
00000e50  27 29 3b 0a 65 6e 64 3b  0a 0a 70 72 6f 63 65 64  |');.end;..proced|
00000e60  75 72 65 20 57 6f 72 64  43 6f 75 6e 74 3b 0a 7b  |ure WordCount;.{|
00000e70  20 61 20 73 6f 6c 75 74  69 6f 6e 20 6f 66 20 70  | a solution of p|
00000e80  72 6f 62 6c 65 6d 20 32  3a 20 63 6f 75 6e 74 20  |roblem 2: count |
00000e90  77 6f 72 64 73 20 69 6e  20 61 20 66 69 6c 65 2e  |words in a file.|
00000ea0  20 7d 0a 7b 20 54 68 69  73 20 69 73 20 64 6f 6e  | }.{ This is don|
00000eb0  65 20 62 79 20 72 65 61  64 69 6e 67 20 69 6e 20  |e by reading in |
00000ec0  63 68 61 72 61 63 74 65  72 73 20 66 72 6f 6d 20  |characters from |
00000ed0  74 68 65 20 66 69 6c 65  0a 20 20 6f 6e 65 20 62  |the file.  one b|
00000ee0  79 20 6f 6e 65 2c 20 74  65 73 74 69 6e 67 20 69  |y one, testing i|
00000ef0  66 20 65 61 63 68 20 63  68 61 72 61 63 74 65 72  |f each character|
00000f00  20 69 73 20 61 20 73 70  61 63 65 2f 43 52 0a 20  | is a space/CR. |
00000f10  20 6f 72 20 6e 6f 74 2c  20 61 6e 64 20 69 6e 63  | or not, and inc|
00000f20  72 65 6d 65 6e 74 69 6e  67 20 74 68 65 20 77 6f  |rementing the wo|
00000f30  72 64 20 63 6f 75 6e 74  20 77 68 65 6e 65 76 65  |rd count wheneve|
00000f40  72 20 69 74 20 70 61 73  73 65 73 0a 20 20 66 72  |r it passes.  fr|
00000f50  6f 6d 20 73 70 61 63 65  20 74 6f 20 61 20 77 6f  |om space to a wo|
00000f60  72 64 2e 20 7d 0a 76 61  72 0a 20 20 46 3a 20 54  |rd. }.var.  F: T|
00000f70  65 78 74 3b 0a 20 20 4e  61 6d 65 3a 20 53 74 72  |ext;.  Name: Str|
00000f80  69 6e 67 3b 0a 20 20 63  3a 20 43 68 61 72 3b 0a  |ing;.  c: Char;.|
00000f90  20 20 43 6f 75 6e 74 3a  20 49 6e 74 65 67 65 72  |  Count: Integer|
00000fa0  3b 0a 20 20 49 6e 57 6f  72 64 2c 20 57 61 73 49  |;.  InWord, WasI|
00000fb0  6e 57 6f 72 64 3a 20 42  6f 6f 6c 65 61 6e 3b 0a  |nWord: Boolean;.|
00000fc0  62 65 67 69 6e 0a 20 20  57 72 69 74 65 4c 6e 28  |begin.  WriteLn(|
00000fd0  27 43 6f 75 6e 74 20 57  6f 72 64 73 27 29 3b 0a  |'Count Words');.|
00000fe0  20 20 57 72 69 74 65 4c  6e 3b 0a 20 20 57 72 69  |  WriteLn;.  Wri|
00000ff0  74 65 28 27 4e 61 6d 65  20 6f 66 20 66 69 6c 65  |te('Name of file|
00001000  20 66 6f 72 20 77 6f 72  64 20 63 6f 75 6e 74 3f  | for word count?|
00001010  20 27 29 3b 0a 20 20 52  65 61 64 4c 6e 28 4e 61  | ');.  ReadLn(Na|
00001020  6d 65 29 3b 0a 20 20 7b  20 6f 70 65 6e 20 74 68  |me);.  { open th|
00001030  65 20 66 69 6c 65 20 66  6f 72 20 72 65 61 64 69  |e file for readi|
00001040  6e 67 20 7d 0a 20 20 41  73 73 69 67 6e 28 46 2c  |ng }.  Assign(F,|
00001050  20 4e 61 6d 65 29 3b 0a  20 20 52 65 73 65 74 28  | Name);.  Reset(|
00001060  46 29 3b 0a 20 20 43 6f  75 6e 74 20 3a 3d 20 30  |F);.  Count := 0|
00001070  3b 0a 20 20 49 6e 57 6f  72 64 20 3a 3d 20 46 61  |;.  InWord := Fa|
00001080  6c 73 65 3b 0a 20 20 77  68 69 6c 65 20 6e 6f 74  |lse;.  while not|
00001090  20 45 4f 46 28 46 29 20  64 6f 0a 20 20 62 65 67  | EOF(F) do.  beg|
000010a0  69 6e 0a 20 20 20 20 57  61 73 49 6e 57 6f 72 64  |in.    WasInWord|
000010b0  20 3a 3d 20 49 6e 57 6f  72 64 3b 0a 20 20 20 20  | := InWord;.    |
000010c0  7b 20 65 6e 64 20 6f 66  20 6c 69 6e 65 20 63 6f  |{ end of line co|
000010d0  75 6e 74 73 20 61 73 20  61 20 77 6f 72 64 20 62  |unts as a word b|
000010e0  72 65 61 6b 20 7d 0a 20  20 20 20 69 66 20 45 4f  |reak }.    if EO|
000010f0  4c 6e 28 46 29 20 74 68  65 6e 0a 20 20 20 20 62  |Ln(F) then.    b|
00001100  65 67 69 6e 0a 20 20 20  20 20 20 52 65 61 64 4c  |egin.      ReadL|
00001110  6e 28 46 29 3b 0a 20 20  20 20 20 20 49 6e 57 6f  |n(F);.      InWo|
00001120  72 64 20 3a 3d 20 46 61  6c 73 65 3b 0a 20 20 20  |rd := False;.   |
00001130  20 65 6e 64 0a 20 20 20  20 65 6c 73 65 0a 20 20  | end.    else.  |
00001140  20 20 62 65 67 69 6e 0a  20 20 20 20 20 20 52 65  |  begin.      Re|
00001150  61 64 28 46 2c 20 63 29  3b 0a 20 20 20 20 20 20  |ad(F, c);.      |
00001160  49 6e 57 6f 72 64 20 3a  3d 20 28 63 20 3c 3e 20  |InWord := (c <> |
00001170  27 20 27 29 3b 0a 20 20  20 20 20 20 7b 20 6e 6f  |' ');.      { no|
00001180  74 20 69 6e 20 61 20 77  6f 72 64 20 69 66 20 63  |t in a word if c|
00001190  20 69 73 20 73 70 61 63  65 20 7d 0a 20 20 20 20  | is space }.    |
000011a0  65 6e 64 3b 0a 20 20 20  20 69 66 20 28 6e 6f 74  |end;.    if (not|
000011b0  20 57 61 73 49 6e 57 6f  72 64 29 20 61 6e 64 20  | WasInWord) and |
000011c0  49 6e 57 6f 72 64 20 74  68 65 6e 0a 20 20 20 20  |InWord then.    |
000011d0  7b 20 70 61 73 73 65 64  20 66 72 6f 6d 20 73 70  |{ passed from sp|
000011e0  61 63 65 20 69 6e 74 6f  20 61 20 77 6f 72 64 20  |ace into a word |
000011f0  7d 0a 20 20 20 20 20 20  43 6f 75 6e 74 20 3a 3d  |}.      Count :=|
00001200  20 43 6f 75 6e 74 20 2b  20 31 3b 0a 20 20 65 6e  | Count + 1;.  en|
00001210  64 3b 0a 20 20 43 6c 6f  73 65 28 46 29 3b 0a 20  |d;.  Close(F);. |
00001220  20 57 72 69 74 65 4c 6e  28 27 54 68 65 20 66 69  | WriteLn('The fi|
00001230  6c 65 20 27 2c 20 4e 61  6d 65 2c 20 27 20 63 6f  |le ', Name, ' co|
00001240  6e 74 61 69 6e 73 20 27  2c 20 43 6f 75 6e 74 3a  |ntains ', Count:|
00001250  31 2c 20 27 20 77 6f 72  64 73 2e 27 29 3b 0a 65  |1, ' words.');.e|
00001260  6e 64 3b 0a 0a 70 72 6f  63 65 64 75 72 65 20 42  |nd;..procedure B|
00001270  75 62 62 6c 65 53 6f 72  74 3b 0a 7b 20 70 72 6f  |ubbleSort;.{ pro|
00001280  62 6c 65 6d 20 33 3a 20  72 65 61 64 20 69 6e 20  |blem 3: read in |
00001290  61 6e 64 20 62 75 62 62  6c 65 20 73 6f 72 74 20  |and bubble sort |
000012a0  6e 75 6d 62 65 72 73 2e  20 7d 0a 63 6f 6e 73 74  |numbers. }.const|
000012b0  20 4d 61 78 20 3d 20 32  30 3b 0a 74 79 70 65 20  | Max = 20;.type |
000012c0  4c 69 73 74 20 3d 20 61  72 72 61 79 5b 31 2e 2e  |List = array[1..|
000012d0  4d 61 78 5d 20 6f 66 20  49 6e 74 65 67 65 72 3b  |Max] of Integer;|
000012e0  0a 0a 20 20 70 72 6f 63  65 64 75 72 65 20 53 77  |..  procedure Sw|
000012f0  61 70 28 76 61 72 20 41  2c 20 42 3a 20 49 6e 74  |ap(var A, B: Int|
00001300  65 67 65 72 29 3b 0a 20  20 76 61 72 20 43 3a 20  |eger);.  var C: |
00001310  49 6e 74 65 67 65 72 3b  0a 20 20 62 65 67 69 6e  |Integer;.  begin|
00001320  0a 20 20 20 20 43 20 3a  3d 20 41 3b 0a 20 20 20  |.    C := A;.   |
00001330  20 41 20 3a 3d 20 42 3b  0a 20 20 20 20 42 20 3a  | A := B;.    B :|
00001340  3d 20 43 3b 0a 20 20 65  6e 64 3b 0a 0a 20 20 70  |= C;.  end;..  p|
00001350  72 6f 63 65 64 75 72 65  20 53 6f 72 74 28 76 61  |rocedure Sort(va|
00001360  72 20 44 61 74 61 3a 20  4c 69 73 74 3b 20 6e 3a  |r Data: List; n:|
00001370  20 49 6e 74 65 67 65 72  29 3b 0a 20 20 76 61 72  | Integer);.  var|
00001380  0a 20 20 20 20 69 2c 20  6a 3a 20 49 6e 74 65 67  |.    i, j: Integ|
00001390  65 72 3b 0a 20 20 20 20  48 61 73 43 68 61 6e 67  |er;.    HasChang|
000013a0  65 64 3a 20 42 6f 6f 6c  65 61 6e 3b 0a 20 20 62  |ed: Boolean;.  b|
000013b0  65 67 69 6e 0a 20 20 20  20 7b 20 62 75 62 62 6c  |egin.    { bubbl|
000013c0  65 73 6f 72 74 20 77 6f  72 6b 73 20 62 79 20 63  |esort works by c|
000013d0  6f 6d 70 61 72 69 6e 67  20 61 64 6a 61 63 65 6e  |omparing adjacen|
000013e0  74 20 76 61 6c 75 65 73  20 6f 66 20 74 68 65 0a  |t values of the.|
000013f0  20 20 20 20 20 20 64 61  74 61 20 61 6e 64 20 73  |      data and s|
00001400  77 61 70 70 69 6e 67 20  74 68 65 6d 20 69 66 20  |wapping them if |
00001410  6e 65 63 65 73 73 61 72  79 2e 20 7d 0a 20 20 20  |necessary. }.   |
00001420  20 69 20 3a 3d 20 31 3b  0a 20 20 20 20 48 61 73  | i := 1;.    Has|
00001430  43 68 61 6e 67 65 64 20  3a 3d 20 54 72 75 65 3b  |Changed := True;|
00001440  0a 20 20 20 20 77 68 69  6c 65 20 28 69 20 3c 20  |.    while (i < |
00001450  6e 29 20 61 6e 64 20 48  61 73 43 68 61 6e 67 65  |n) and HasChange|
00001460  64 20 64 6f 0a 20 20 20  20 62 65 67 69 6e 0a 20  |d do.    begin. |
00001470  20 20 20 20 20 48 61 73  43 68 61 6e 67 65 64 20  |     HasChanged |
00001480  3a 3d 20 46 61 6c 73 65  3b 0a 20 20 20 20 20 20  |:= False;.      |
00001490  66 6f 72 20 6a 20 3a 3d  20 6e 20 2d 20 31 20 64  |for j := n - 1 d|
000014a0  6f 77 6e 74 6f 20 69 20  64 6f 0a 20 20 20 20 20  |ownto i do.     |
000014b0  20 20 20 69 66 20 44 61  74 61 5b 6a 20 2b 20 31  |   if Data[j + 1|
000014c0  5d 20 3c 20 44 61 74 61  5b 6a 5d 20 74 68 65 6e  |] < Data[j] then|
000014d0  0a 20 20 20 20 20 20 20  20 62 65 67 69 6e 0a 20  |.        begin. |
000014e0  20 20 20 20 20 20 20 20  20 53 77 61 70 28 44 61  |         Swap(Da|
000014f0  74 61 5b 6a 20 2b 20 31  5d 2c 20 44 61 74 61 5b  |ta[j + 1], Data[|
00001500  6a 5d 29 3b 0a 20 20 20  20 20 20 20 20 20 20 48  |j]);.          H|
00001510  61 73 43 68 61 6e 67 65  64 20 3a 3d 20 54 72 75  |asChanged := Tru|
00001520  65 3b 0a 20 20 20 20 20  20 20 20 65 6e 64 3b 0a  |e;.        end;.|
00001530  20 20 20 20 20 20 69 20  3a 3d 20 69 20 2b 20 31  |      i := i + 1|
00001540  3b 0a 20 20 20 20 65 6e  64 3b 0a 20 20 65 6e 64  |;.    end;.  end|
00001550  3b 0a 0a 76 61 72 0a 20  20 6e 2c 20 78 3a 20 49  |;..var.  n, x: I|
00001560  6e 74 65 67 65 72 3b 0a  20 20 44 61 74 61 3a 20  |nteger;.  Data: |
00001570  4c 69 73 74 3b 0a 62 65  67 69 6e 0a 20 20 57 72  |List;.begin.  Wr|
00001580  69 74 65 4c 6e 28 27 42  75 62 62 6c 65 73 6f 72  |iteLn('Bubblesor|
00001590  74 20 6f 66 20 6e 75 6d  62 65 72 73 27 29 3b 0a  |t of numbers');.|
000015a0  20 20 57 72 69 74 65 4c  6e 3b 0a 20 20 57 72 69  |  WriteLn;.  Wri|
000015b0  74 65 4c 6e 28 27 54 79  70 65 20 69 6e 20 75 70  |teLn('Type in up|
000015c0  20 74 6f 20 32 30 20 6e  75 6d 62 65 72 73 2c 20  | to 20 numbers, |
000015d0  65 6e 64 69 6e 67 20 77  69 74 68 20 2d 39 39 39  |ending with -999|
000015e0  3a 27 29 3b 0a 20 20 6e  20 3a 3d 20 30 3b 0a 20  |:');.  n := 0;. |
000015f0  20 72 65 70 65 61 74 0a  20 20 20 20 52 65 61 64  | repeat.    Read|
00001600  4c 6e 28 78 29 3b 0a 20  20 20 20 69 66 20 28 78  |Ln(x);.    if (x|
00001610  20 3c 3e 20 2d 39 39 39  29 20 61 6e 64 20 28 6e  | <> -999) and (n|
00001620  20 3c 20 32 30 29 20 74  68 65 6e 0a 20 20 20 20  | < 20) then.    |
00001630  62 65 67 69 6e 0a 20 20  20 20 20 20 6e 20 3a 3d  |begin.      n :=|
00001640  20 6e 20 2b 20 31 3b 0a  20 20 20 20 20 20 44 61  | n + 1;.      Da|
00001650  74 61 5b 6e 5d 20 3a 3d  20 78 3b 0a 20 20 20 20  |ta[n] := x;.    |
00001660  65 6e 64 3b 0a 20 20 75  6e 74 69 6c 20 78 20 3d  |end;.  until x =|
00001670  20 2d 39 39 39 3b 0a 20  20 53 6f 72 74 28 44 61  | -999;.  Sort(Da|
00001680  74 61 2c 20 6e 29 3b 0a  20 20 57 72 69 74 65 4c  |ta, n);.  WriteL|
00001690  6e 28 27 54 68 65 20 27  2c 20 6e 3a 31 2c 20 27  |n('The ', n:1, '|
000016a0  20 73 6f 72 74 65 64 20  6e 75 6d 62 65 72 73 20  | sorted numbers |
000016b0  61 72 65 3a 27 29 3b 0a  20 20 66 6f 72 20 78 20  |are:');.  for x |
000016c0  3a 3d 20 31 20 74 6f 20  6e 20 64 6f 0a 20 20 57  |:= 1 to n do.  W|
000016d0  72 69 74 65 4c 6e 28 44  61 74 61 5b 78 5d 29 3b  |riteLn(Data[x]);|
000016e0  0a 65 6e 64 3b 0a 0a 70  72 6f 63 65 64 75 72 65  |.end;..procedure|
000016f0  20 4d 65 6e 75 3b 0a 7b  20 74 68 65 20 70 72 6f  | Menu;.{ the pro|
00001700  67 72 61 6d 27 73 20 6d  61 69 6e 20 6d 65 6e 75  |gram's main menu|
00001710  2c 20 61 6c 6c 6f 77 69  6e 67 20 63 68 6f 69 63  |, allowing choic|
00001720  65 20 6f 66 20 70 72 6f  62 6c 65 6d 73 20 74 6f  |e of problems to|
00001730  20 74 65 73 74 2e 20 7d  0a 76 61 72 20 6f 3a 20  | test. }.var o: |
00001740  49 6e 74 65 67 65 72 3b  0a 62 65 67 69 6e 0a 20  |Integer;.begin. |
00001750  20 72 65 70 65 61 74 0a  20 20 20 20 57 72 69 74  | repeat.    Writ|
00001760  65 4c 6e 28 27 43 68 6f  6f 73 65 20 61 20 6d 65  |eLn('Choose a me|
00001770  6e 75 20 6f 70 74 69 6f  6e 3a 27 29 3b 0a 20 20  |nu option:');.  |
00001780  20 20 57 72 69 74 65 4c  6e 3b 0a 20 20 20 20 57  |  WriteLn;.    W|
00001790  72 69 74 65 4c 6e 28 27  31 29 20 50 72 6f 62 6c  |riteLn('1) Probl|
000017a0  65 6d 20 31 3a 20 46 69  6e 64 20 70 72 69 6d 65  |em 1: Find prime|
000017b0  20 6e 75 6d 62 65 72 73  2e 27 29 3b 0a 20 20 20  | numbers.');.   |
000017c0  20 57 72 69 74 65 4c 6e  28 27 32 29 20 50 72 6f  | WriteLn('2) Pro|
000017d0  62 6c 65 6d 20 32 3a 20  57 6f 72 64 20 43 6f 75  |blem 2: Word Cou|
000017e0  6e 74 2e 27 29 3b 0a 20  20 20 20 57 72 69 74 65  |nt.');.    Write|
000017f0  4c 6e 28 27 33 29 20 50  72 6f 62 6c 65 6d 20 33  |Ln('3) Problem 3|
00001800  3a 20 42 75 62 62 6c 65  73 6f 72 74 2e 27 29 3b  |: Bubblesort.');|
00001810  0a 20 20 20 20 57 72 69  74 65 4c 6e 28 27 34 29  |.    WriteLn('4)|
00001820  20 45 78 69 74 20 74 68  65 20 70 72 6f 67 72 61  | Exit the progra|
00001830  6d 2e 27 29 3b 0a 20 20  20 20 57 72 69 74 65 4c  |m.');.    WriteL|
00001840  6e 3b 0a 20 20 20 20 57  72 69 74 65 28 27 43 68  |n;.    Write('Ch|
00001850  6f 69 63 65 3f 20 27 29  3b 0a 20 20 20 20 52 65  |oice? ');.    Re|
00001860  61 64 4c 6e 28 6f 29 3b  0a 20 20 20 20 63 61 73  |adLn(o);.    cas|
00001870  65 20 6f 20 6f 66 0a 20  20 20 20 20 20 31 3a 20  |e o of.      1: |
00001880  50 72 69 6d 65 73 3b 0a  20 20 20 20 20 20 32 3a  |Primes;.      2:|
00001890  20 57 6f 72 64 43 6f 75  6e 74 3b 0a 20 20 20 20  | WordCount;.    |
000018a0  20 20 33 3a 20 42 75 62  62 6c 65 53 6f 72 74 3b  |  3: BubbleSort;|
000018b0  0a 20 20 20 20 20 20 34  3a 20 57 72 69 74 65 4c  |.      4: WriteL|
000018c0  6e 28 27 45 78 69 74 69  6e 67 2e 2e 2e 27 29 3b  |n('Exiting...');|
000018d0  0a 20 20 20 20 65 6c 73  65 0a 20 20 20 20 20 20  |.    else.      |
000018e0  57 72 69 74 65 4c 6e 28  6f 2c 20 27 20 69 73 20  |WriteLn(o, ' is |
000018f0  6e 6f 74 20 61 20 76 61  6c 69 64 20 6d 65 6e 75  |not a valid menu|
00001900  20 6f 70 74 69 6f 6e 2e  27 29 3b 0a 20 20 20 20  | option.');.    |
00001910  65 6e 64 3b 0a 20 20 20  20 57 72 69 74 65 4c 6e  |end;.    WriteLn|
00001920  3b 0a 20 20 20 20 69 66  20 6f 20 3c 3e 20 34 20  |;.    if o <> 4 |
00001930  74 68 65 6e 0a 20 20 20  20 62 65 67 69 6e 0a 20  |then.    begin. |
00001940  20 20 20 20 20 57 72 69  74 65 28 27 50 72 65 73  |     Write('Pres|
00001950  73 20 3c 65 6e 74 65 72  3e 20 74 6f 20 63 6f 6e  |s <enter> to con|
00001960  74 69 6e 75 65 2e 2e 2e  27 29 3b 0a 20 20 20 20  |tinue...');.    |
00001970  20 20 52 65 61 64 4c 6e  3b 0a 20 20 20 20 65 6e  |  ReadLn;.    en|
00001980  64 3b 0a 20 20 20 20 57  72 69 74 65 4c 6e 3b 0a  |d;.    WriteLn;.|
00001990  20 20 75 6e 74 69 6c 20  6f 20 3d 20 34 3b 0a 65  |  until o = 4;.e|
000019a0  6e 64 3b 0a 0a 62 65 67  69 6e 0a 20 20 7b 20 6d  |nd;..begin.  { m|
000019b0  61 69 6e 20 70 72 6f 67  72 61 6d 20 7d 0a 20 20  |ain program }.  |
000019c0  57 72 69 74 65 4c 6e 3b  0a 20 20 57 72 69 74 65  |WriteLn;.  Write|
000019d0  4c 6e 28 27 2a 2a 2a 2a  2a 2a 2a 2a 2a 2a 2a 2a  |Ln('************|
000019e0  2a 2a 2a 2a 2a 2a 2a 2a  2a 2a 2a 2a 2a 2a 2a 2a  |****************|
*
00001a00  2a 2a 2a 2a 2a 2a 2a 2a  2a 2a 2a 27 29 3b 0a 20  |***********');. |
00001a10  20 57 72 69 74 65 4c 6e  28 27 2a 20 20 20 20 20  | WriteLn('*     |
00001a20  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
*
00001a50  2a 27 29 3b 0a 20 20 57  72 69 74 65 4c 6e 28 27  |*');.  WriteLn('|
00001a60  2a 20 20 42 72 69 74 69  73 68 20 49 6e 66 6f 72  |*  British Infor|
00001a70  6d 61 74 69 63 73 20 4f  6c 79 6d 70 69 61 64 20  |matics Olympiad |
00001a80  31 39 39 35 20 20 20 20  20 20 20 20 20 20 20 20  |1995            |
00001a90  20 20 20 20 20 20 2a 27  29 3b 0a 20 20 57 72 69  |      *');.  Wri|
00001aa0  74 65 4c 6e 28 27 2a 20  20 3d 3d 3d 3d 3d 3d 3d  |teLn('*  =======|
00001ab0  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 3d 3d 3d 3d 3d 3d  |================|
00001ac0  3d 3d 3d 3d 3d 3d 3d 3d  3d 3d 20 20 20 20 20 20  |==========      |
00001ad0  20 20 20 20 20 20 20 20  20 20 20 20 2a 27 29 3b  |            *');|
00001ae0  0a 20 20 57 72 69 74 65  4c 6e 28 27 2a 20 20 20  |.  WriteLn('*   |
00001af0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
*
00001b20  20 20 2a 27 29 3b 0a 20  20 57 72 69 74 65 4c 6e  |  *');.  WriteLn|
00001b30  28 27 2a 20 20 53 61 6d  70 6c 65 20 70 72 6f 62  |('*  Sample prob|
00001b40  6c 65 6d 73 20 20 20 20  20 20 20 20 2d 20 20 20  |lems        -   |
00001b50  20 20 20 28 43 29 20 41  6e 74 6f 6e 79 20 52 69  |   (C) Antony Ri|
00001b60  78 20 31 39 39 35 20 20  2a 27 29 3b 0a 20 20 57  |x 1995  *');.  W|
00001b70  72 69 74 65 4c 6e 28 27  2a 20 20 51 75 65 73 74  |riteLn('*  Quest|
00001b80  69 6f 6e 20 73 74 61 74  65 6d 65 6e 74 20 61 6e  |ion statement an|
00001b90  64 20 65 78 61 6d 70 6c  65 20 73 6f 6c 75 74 69  |d example soluti|
00001ba0  6f 6e 20 69 6e 20 50 61  73 63 61 6c 20 20 2a 27  |on in Pascal  *'|
00001bb0  29 3b 0a 20 20 57 72 69  74 65 4c 6e 28 27 2a 20  |);.  WriteLn('* |
00001bc0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
*
00001bf0  20 20 20 20 2a 27 29 3b  0a 20 20 57 72 69 74 65  |    *');.  Write|
00001c00  4c 6e 28 27 2a 2a 2a 2a  2a 2a 2a 2a 2a 2a 2a 2a  |Ln('************|
00001c10  2a 2a 2a 2a 2a 2a 2a 2a  2a 2a 2a 2a 2a 2a 2a 2a  |****************|
*
00001c30  2a 2a 2a 2a 2a 2a 2a 2a  2a 2a 2a 27 29 3b 0a 20  |***********');. |
00001c40  20 57 72 69 74 65 4c 6e  3b 0a 20 20 4d 65 6e 75  | WriteLn;.  Menu|
00001c50  3b 0a 65 6e 64 2e 0a                              |;.end..|
00001c57