You are on page 1of 2

CS204 Lab 1 : Simple sorting

Log on to a Unix machine that you have access to.

Your first task is to open a terminal on the Linux machine. A terminal


is a program that allows you to type Unix commands and run
programs. Not every version of Linux opens a terminal automatically
when you log in, and unfortunately different versions of Linux use
different terminal programs.

On many versions of Linux typing Alt-F2 will bring up a command prompt


mini window at the top of the screen. Type "terminal" into that prompt,
and (hopefully) you will be given a range of terminal programs to choose
from, such as Konsole, gnome-terminal, terminator, or xterm. If you
select one of these then a terminal window should appear where you
can start work.

Once you have the terminal open, you can see a list of your files with:
ls
Create a directory for cs2014 lab work:
mkdir cs204
Now switch to that directory:
cd cs204
Create a directory for your first lab:
mkdir lab1
Now switch to that directory
cd lab1

Download the following files to that directory.

http://www.cs.tcd.ie/David.Gregg/cs2014/labs/sorts.c
http://www.cs.tcd.ie/David.Gregg/cs2014/labs/numbers.txt

On many (but not all) Unix machines you can use the wget command
to download files. For example:
wget http://www.cs.tcd.ie/David.Gregg/cs2014/labs/sorts.c

Open the C program using your favorite editor, such as gedit, nano,
vim or emacs. If you are working on one of the Linux machines in LG12,
]you can run gedit as follows:
gedit sorts.c &

The & at the end of the command tells the terminal to run the gedit
program, but not to wait for it to complete. This allows you to
type more commands before the gedit command completes. This is
what you want on a Linux machine running X-windows.

If you are running your terminal from a machine that is not running
X-windows, then you probably want to run your editor inside the
terminal program. In this case, you would use a simpler editor
such as nano, which runs inside the terminal.
nano sorts.c

Note that when you are using a terminal editor like nano, you
don't want to have the & character at the end of the command,
because the editor is running within the terminal. In this
case you will need a second terminal to compile and run your
program.

To compile your program type one of the following:


clang -o sorts sorts.c
gcc -o sorts sorts.c
cc -o sorts sorts.c

These are various C compilers. The standard C compiler on Unix systems


is called cc. Gcc is the GNU C compiler. On most Linux systems, the
gcc compiler is the standard compiler, so cc is just another name for
gcc. Clang is a relatively new C compiler. Its error messages are
usually much easier to understand than the error messages in
gcc. Typically gcc is installed on almost any Unix system, whereas
clang is rarer. However, clang is becoming increasingly popular
because its error messages are so much better than gcc's.

To run your program type:


./sorts

The file 'sorts.c' contains a simple program to read in some numbers


from a text file, sort them into increasing order, and write them to
the screen. The file 'numbers.txt' contains some numbers to sort.

The existing sorts.c program uses the "funny" sort algorithm to sort
the numbers. In addition there are prototypes, but no code for bubble
sort, selection sort and insertion sort. Write the code for these
sorts, and test it out in this program. For selection sort, you can
use the existing findMin function (if you want).

Next modify each sorting function so that it returns the number of


times that the inner loop of the sort is executed. Modify the main()
function so that it sorts the same original array with each sorting
algorithm, and writes out the number of iterations used by each
algorithm.

The purpose of this lab is purely to get familiar with Unix-like


systems, the C compiler, and the C syntax. There is no need to submit
any work for marking, or to demo your work. We'll start marked labs
next week.

You might also like