Week 01 Laboratory Exercises

Objectives

  • to refamiliarise yourself with C
  • to use the linux manual
  • to practice using standard I/O
  • to practice interacting with command-line arguments
  • to explore implementing recursive functions

Preparation

Before the lab you should re-read the relevant lecture slides and their accompanying examples.

Getting Started

Set up for the lab by creating a new directory called lab01 and changing to this directory.
mkdir lab01
cd lab01

There are some provided files for this lab which you can fetch with this command:

1092 fetch lab01

If you're not working at CSE, you can download the provided files as a zip file or a tar file.

Exercise — individual:
Remove All Vowels from STDIN

Write a C program no_vowels.c which reads characters from its input and writes the same characters to its output, except it does not write vowels.

Your program should stop only at the end of input.

no_vowels.c must only use:
scanf(3) to read one character at a time from stdin.
printf(3) to print one character at a time to stdout.

Once you have no_vowels.c working correctly it should behave as follows:

./no_vowels
Hello, World!
Hll, Wrld!

echo "Peter Piper picked a peck of pickled peppers." > input
./no_vowels < input
Ptr Ppr pckd  pck f pckld ppprs.
./no_vowels
Andrew is the LiC of COMP1521
ndrw s th LC f CMP1521
Are you saying 'Boo' or 'Boo-Urns'?
r y syng 'B' r 'B-rns'?
In this house, we obey the laws of thermodynamics!
n ths hs, w by th lws f thrmdynmcs!


When you think your program is working, you can use autotest to run some simple automated tests:

1092 autotest no_vowels 

When you are finished working on this exercise, you must submit your work by running give:

give dp1092 lab01_no_vowels no_vowels.c

You must run give before Tuesday 03 September 09:00 (2024-09-03 09:00:00) to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise — individual:
Transform All Uppercase letters to Lowercase

Write a C program no_uppercase.c which reads characters from its input and writes the same characters to its output, any upper case letters are replaced by their as lower case equivalent.

Your program should stop only at the end of input.

Add code to no_uppercase.c so that, given text on stdin, any uppercase letters are printed as lowercase to stdout.

no_uppercase.c must only use:
getchar(3) to read one character at a time from stdin.
putchar(3) to print one character at a time to stdout.

Once you have no_uppercase.c working correctly it should behave as follows:

./no_uppercase
ABC
abc
ABCabc123
abcabc123
123!@#
123!@#
Hello, World!
hello, world!
Andrew is the LiC of COMP1521
andrew is the lic of comp1521

echo "Peter Piper picked a peck of pickled peppers." > input
./no_uppercase < input
peter piper picked a peck of pickled peppers.

When you think your program is working, you can use autotest to run some simple automated tests:

1092 autotest no_uppercase 

When you are finished working on this exercise, you must submit your work by running give:

give dp1092 lab01_no_uppercase no_uppercase.c

You must run give before Tuesday 03 September 09:00 (2024-09-03 09:00:00) to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise — individual:
Remove Uneven Lines of Input

Add code to no_odd_lines.c so that, given text on stdin, only print lines with an even number of characters to stdout.

no_odd_lines.c must only use:
fgets(3) to read one line at a time from stdin.
fputs(3) to print one line at a time to stdout.

Once you have no_odd_lines.c working correctly it should behave as follows:

./no_odd_lines
Hello, World
Hello, World!
Hello, World!
Sri is the LiC of COMP1521
Sri is the LiC of COMP1521, and Jashank is the admin.
Sri is the LiC of COMP1521, and Jashank is the admin.

echo "Peter Piper picked a peck of pickled peppers." > input
echo "A peck of pickled peppers Peter Piper picked." >> input
echo "If Peter Piper picked a peck of pickled peppers," >> input
echo "Where's the peck of pickled peppers Peter Piper picked?" >> input
./no_odd_lines < input
Peter Piper picked a peck of pickled peppers.
A peck of pickled peppers Peter Piper picked.
Where's the peck of pickled peppers Peter Piper picked?

When you think your program is working, you can use autotest to run some simple automated tests:

1092 autotest no_odd_lines 

When you are finished working on this exercise, you must submit your work by running give:

give dp1092 lab01_no_odd_lines no_odd_lines.c

You must run give before Tuesday 03 September 09:00 (2024-09-03 09:00:00) to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise — individual:
Pretty Print Command Line Arguments

Add code to my_args.c so that, given 0 or more command line arguments, the command line arguments are "pretty printed".

Once you have my_args.c working correctly it should behave as follows:

./my_args
Program name: ./my_args
There are no other arguments
../lab01/my_args
Program name: ../lab01/my_args
There are no other arguments
./my_args hello world
Program name: ./my_args
There are 2 arguments:
	Argument 1 is "hello"
	Argument 2 is "world"
./my_args "hello world" 1 2 3 4 5
Program name: ./my_args
There are 6 arguments:
	Argument 1 is "hello world"
	Argument 2 is "1"
	Argument 3 is "2"
	Argument 4 is "3"
	Argument 5 is "4"
	Argument 6 is "5"

When you think your program is working, you can use autotest to run some simple automated tests:

1092 autotest my_args 

When you are finished working on this exercise, you must submit your work by running give:

give dp1092 lab01_my_args my_args.c

You must run give before Tuesday 03 September 09:00 (2024-09-03 09:00:00) to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise — individual:
Statistical Analysis of Command Line Arguments

Add code to arg_stats.c so that, given 1 or more command line arguments, it prints the minimum and maximum values, the sum and product of all the values, and the mean of all the values.

Once you have arg_stats.c working correctly it should behave as follows:

./arg_stats
Usage: ./arg_stats NUMBER [NUMBER ...]
./arg_stats 1
MIN:  1
MAX:  1
SUM:  1
PROD: 1
MEAN: 1
./arg_stats 1 2 3 4 5 6 7 8 9
MIN:  1
MAX:  9
SUM:  45
PROD: 362880
MEAN: 5
./arg_stats 9 8 7 6 1 2 3 5 4
MIN:  1
MAX:  9
SUM:  45
PROD: 362880
MEAN: 5
./arg_stats 1 9 1 9 1 9
MIN:  1
MAX:  9
SUM:  30
PROD: 729
MEAN: 5

When you think your program is working, you can use autotest to run some simple automated tests:

1092 autotest arg_stats 

When you are finished working on this exercise, you must submit your work by running give:

give dp1092 lab01_arg_stats arg_stats.c

You must run give before Tuesday 03 September 09:00 (2024-09-03 09:00:00) to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise — individual:
(Dis)Proving the Collatz Conjecture

Add code to collatz.c that take a single positive integer as a command-line argument and prints the collatz chain for that number.

This is how the collatz chain is calculated:

If the current number is 1, the series terminates.
If the current number is ODD, then multiply by 3 and add 1 to get the next number
If the current number is EVEN, then divide by 2 to get the next number

collatz function defined in modular arithmetic notation

Once you have collatz.c working correctly it should behave as follows:

./collatz
Usage: ./collatz NUMBER
./collatz 1
1
./collatz 12
12
6
3
10
5
16
8
4
2
1
./collatz 10
10
5
16
8
4
2
1

When you think your program is working, you can use autotest to run some simple automated tests:

1092 autotest collatz 

When you are finished working on this exercise, you must submit your work by running give:

give dp1092 lab01_collatz collatz.c

You must run give before Tuesday 03 September 09:00 (2024-09-03 09:00:00) to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Exercise — individual:
Calculating the Fibonacci Sequence The (Not So) Fast Way

Add code to fibonacci.c so that, given a line of input containing a natural number the corresponding Fibonacci number is printed.

This is how you obtain your Fibonacci number:

Starting with 0 and 1:
Add the preceding two Fibonacci numbers together to get the current Fibonacci number:

fibonacci function define in modular arithmetic notation
fibonacci function define in modular arithmetic notation

Once you have fibonacci.c working correctly it should behave as follows - note that bolded lines represent sample user input to the program, and the other lines represent the expected output. That is, an input of 2 prints 1, because 1 is the second Fibonacci number, and an input of 9 prints 34, because 34 is the ninth Fibonacci number.

./fibonacci
0
0
1
1
2
1
5
5
9
34


When you think your program is working, you can use autotest to run some simple automated tests:

1092 autotest fibonacci 

When you are finished working on this exercise, you must submit your work by running give:

give dp1092 lab01_fibonacci fibonacci.c

You must run give before Tuesday 03 September 09:00 (2024-09-03 09:00:00) to obtain the marks for this lab exercise. Note that this is an individual exercise, the work you submit with give must be entirely your own.

Submission

When you are finished each exercises make sure you submit your work by running give.

You can run give multiple times. Only your last submission will be marked.

Don't submit any exercises you haven't attempted.

If you are working at home, you may find it more convenient to upload your work via give's web interface.

Remember you have until Week 2 Tuesday 09:00:00 to submit your work.

You cannot obtain marks by e-mailing your code to tutors or lecturers.

You check the files you have submitted here.

Automarking will be run by the lecturer several days after the submission deadline, using test cases different to those autotest runs for you. (Hint: do your own testing as well as running autotest.)

After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via give's web interface.

Lab Marks

When all components of a lab are automarked you should be able to view the the marks via give's web interface or by running this command on a CSE machine:

1092 classrun -sturec