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:

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:

[cc lang=”pascal”]program Hello;

begin

Writeln(‘Hello World’);

end.

Compile with fpc hello.pas and run:

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:

[cc lang=”pascal”]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)

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.

You May Also Like

Click a bunch of times – using AutoHotKey

Let’s say you use a piece of software which is horribly designed…

Visual.Syntax is my choice for code highlighting

I am using the Visual.Syntax code highlighting plugin by Matthew Delmarter. There…

Apt-get Update GPG Key Errors and Fix

Running sudo apt-get upgrade, I started getting this error: Reading package lists……