ANU College of Engineering and Computer Science
Research School of Computer Science
Laboratory 0 (this is an optional lab)
Note that these lab notes for labs 1 to 5 were developed originally by Andrew Trigdell and then refined by Bob Edwards. Some minor modifications have also be made by myself (Eric McCreath).
This laboratory should take you less than 2 hours to complete. Make sure you ask your tutor for help if you get stuck. You are expected to print out and read through these instructions and to complete about 1 hour of preparation. This lab is mainly for people who would like a bit more C practice.
The main objective of this lab is to gain some practice in writing C code. This includes:
Many people will have had some exposure to C and for those people this should be a straight-forward lab. However, if you have not done this before then you will need to do a lot more preparation. Also, remember to use the 'man' pages for detail about standard library routines.
If possible it is helpful to get hold of an old C book. Any would do. Have a look through it and remind yourself of some C code. Also have a read through some C programs and familiarize yourself with the syntax (you could use the Linux kernel for this).
Go through the steps of the lab and write out (on paper or in a text editor) the code for the problems given in the lab. This means that during the lab you can focus on debugging and running the programs.
During the Lab
Step 1 - Hello World!
Write a program that takes a list of names (each on a separate line) and says hello to each person. Input should come from stdin and output should go to stdout. So for example if I put my program in the file 'hello.c' I could have the following interaction with the program:
% gcc -Wall -o hello hello.c % hello Jim Hello Jim! Fred Hello Fred! ^D Bye
Optional Extra - If the program sees your name get it to say 'Hi' instead of 'Hello'.
Step 2 - marks list
Write a program that takes a list of names and marks. The program then prints out the following:
Each entry (name and mark) should be stored in a struct. These structures should be linked together in a linked list (this will involve malloc and pointers!).
Now if an interaction should go something like this:
% gcc -Wall -o marks marks.c % hello Jim 10 Fred 13 Sue 20 Kate 12 ^D Sue has the highest mark. The students are: Kate 12 Sue 20 Fred 13 Jim 10
Step 3 - ltrace and strace
Run both ltrace and strace on each of your programs. What calls are made for I/O? Which calls are made for memory allocation?
Within the structure from step 2 add a pointer to a function which takes an integer as input and return an integer. When the data is read in, assign this pointer to either a routine that adds 3 to the number or subtracts 3 (alternate between these two). Now when you print the list of names and marks also show the mark with this function is applied.
The output from above should now be:
Sue has the highest mark. The students are: Kate 12 -> 9 Sue 20 -> 23 Fred 13 -> 10 Jim 10 -> 13