Experimenting with Pascal on Ubuntu

pascal


I’ve been busy lately on a number of projects, one of which is a programming class I am currently taking. The class itself is interesting, we are learning about the different types of programming languages. For our latest project, we were tasked with writing a simple program in Pascal. Pascal isn’t used too much any more since it lacks some of the features that most modern languages have, but it is good to know at least a little bit about it in case you ever run across some old Pascal programs in the wild.

The syntax for pascal is a bit verbose, that is the main complaint about it. There are a number of others, but that is beyond the scope of this howto.

Installing The Pascal Compiler on Ubuntu

Installing Pascal in modern Ubuntu is a cinch. The Free Pascal Compiler, or fpc, is all that you need to get started. It works great on 32-bit or 64-bit systems. Install with:

1
sudo apt-get install fpc

Any prerequisites will automatically download and install along with fpc.

Getting Started in Pascal

To test the compiler let’s start with a simple Hello World program. Open up hello.pas and enter:

1
2
3
4
5
6
7
program Hello;

begin

Writeln('Hello World');

end.

Compile with fpc hello.pas and run:

1
2
dave@cerberus:~/Pascal$ ./hello
Hello World

Selection Sort in Pascal

Now that we’ve verified it is running, I’m going to show you the code that I wrote for my program. Basically we were asked to Selection Sort two arrays of varying length. Apparently one of the (bad) features of Pascal originally was that you needed to declare the length of the array which made it a pain to work with them.

In this situation it is just two arrays so it isn’t too bad. Enter your array by creating two text files arrayA.txt and arrayB.txt. One number per line. The source code for sort.pas is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
program Sort;

var
        A: array[1..10] of Integer;
        B: array[1..20] of Integer;
        F: Text;
        i,j,k,l,m,temp: Integer;

begin
        {Read in array A}
        Assign(F, 'arrayA.txt');
        Reset(F);
        i:= 0;
        while not EOF(F) do begin
                Inc(i);
                Read(F, A[i]);
        end;

        {Read in array B}
        Assign(F, 'arrayB.txt');
        Reset(F);
        j:= 0;
        while not EOF(F) do begin
                Inc(j);
                Read(F, B[j]);
        end;
        i:=10;
        j:=20;

        {Print out the unsorted arrays}
        WriteLn('Unsorted Arrays:');
        WriteLn('Array A:');
        for k:=1 to i do
        Write(A[k], ' ');
        WriteLn();
        WriteLn('Array B:');
        for k:=1 to j do
        Write(B[k], ' ');
        WriteLn();
        WriteLn('=========================');
        WriteLn('Sorting Arrays...');
        WriteLn('=========================');

        {Selection Sort Array A}
        for l := 1 to i do
                for m := l + 1 to i do
                        if A[l] > A[m] then
                        begin
                                temp := A[l];
                                A[l] := A[m];
                                A[m] := temp;
                        end;
        {Selection Sort Array B}
        for l := 1 to j do
                for m := l + 1 to j do
                        if B[l] > B[m] then
                        begin
                                temp := B[l];
                                B[l] := B[m];
                                B[m] := temp;
                        end;

        {Print out the sorted arrays}
        WriteLn('Selection Sorted Arrays:');
        WriteLn('Array A: ');
        for k:=1 to i do
        Write(A[k], ' ');
        WriteLn();
        WriteLn('Array B: ');
        for k:=1 to j do
        Write(B[k], ' ');
        WriteLn();
end.

Compile and run (ok to ignore the compile-time errors)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
dave@cerberus:~/Pascal$ fpc sort.pas
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling sort.pas
Linking sort
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
73 lines compiled, 0.1 sec
dave@cerberus:~/Pascal$ ./sort
Unsorted Arrays:
Array A:
28 24 85 55 43 6 23 13 59 71
Array B:
13 37 36 53 24 83 27 42 62 71 9 92 1 41 6 3 88 77 65 67
=========================
Sorting Arrays...
=========================
Selection Sorted Arrays:
Array A:
6 13 23 24 28 43 55 59 71 85
Array B:
1 3 6 9 13 24 27 36 37 41 42 53 62 65 67 71 77 83 88 92
dave@cerberus:~/Pascal$

And there you have it. Compiling Pascal program on Ubuntu is an easy way to get your feet wet in programming. Pascal is a great beginner’s programming language, but if you want to learn more there are a number of great resources available for learning Pascal.