|
Solutions for Labsheet 5 - for the week commencing 25th September 2023
This page provides some sample solutions and discussion
on some of the tasks of Labsheet-5.
Tasks
- Write a C program, named listints, which will print
the integers requested via a single command-line argument.
The list of integers is to appear in strictly increasing order,
with each requested integer appearing once and only once
(even if duplicated in the request).
- Locate a single-file program consisting of several functions,
such as the one developed in
Workshop-3.
Now, break the single-file program into a multi-file project consisting of:
- a single C header file
(providing the function declarations),
and
- several C source code files
(with each C file providing at least one function definition).
Next, develop a simple Makefile,
as introduced in Lecture-17,
to compile and link your multi-file project using make.
Determine how to test that your Makefile correctly represents
the dependencies between your source and header files.
Extend your Makefile by adding a new target to cleanup (remove)
any unnecessary files (careful!).
- Both Linux and macOS
provide a standard command named wc (an abbreviation for
wordcount!) which determines the number of lines, words, and characters
in a named file.
You can read about this command using the online documentation:
(man wc).
For this task,
you will develop your own version of the wc program named mywc.
-
🌶
Now, using the standard getopt() function introduced in
Lecture 17,
add support for command-line options to your mywc utility from Task-3.
Add command-line options to request:
- -c to report the number of characters,
- -l to report the number of lines,
- -L to report the maximum line length (as supported on Linux, but not macOS), and
- -w to report the number of words.
|