COMP 348: ASSIGNMENT 1
Note that assignments must be submitted on time in order to receive
full value. Appropriate late penalties (< 1 hour = 10%, < 24 hours =
25%) will be applied, as appropriate.
DESCRIPTION: In this assignment, you will gain hands-on
experience with the C programming language. While you are
not required to be a C expert to complete the work, you will
certainly have the opportunity to explore most of the things
that we have discussed in class (and a few other things as
well).
In terms of your task, it is fairly easy to describe. You will be writing a simple search and sort
application. Specifically, your job is to (1) read a text file into an array of strings, (2) search for a
particular word, (3) sort the input, and (4) display the result in the output. It will work as
follows:
1. The application will take command line arguments. These arguments are interpreted
as a text strings. For example, if your program was called ssort.exe, you might enter
ssort.exe <inputfile> <n> <wtype> <sorttype> [<skipword1> <skipword2>
<skipword3> …]
2. Your program opens the input file and reads a series of words into an array. The
array size is specified by <n>. Note that you need to convert <n> into a valid integer.
In case n is not specified, the program must display an error message (on the
standard error); and terminate abnormally (error code 1).
3. <wtype> specifies the word type. They are as follows:
a. ALPHA: only English words composed of [a-z] and [A-Z].
b. ALPHANUM: all words composed by alphanumeric characters.
c. ALL: all words separated by whitespaces: blank, tab, null, vertical tab, … as
well as the comma character as separator.
Examples of words are: $, apple_1, …
Note that <wtype> is also a required parameter, and in case it is missing, program
must display a proper error message; and terminate abnormally (error code 2).
Also note that there is no null/empty word. For example a line containing
“a,,apple,,,test” (with options ALPHA, ALPHANUM, or ALL) contains three word
only: a, apple, and test.
1
4. <sorttype> specifies the ordering of the words. Possible values: ASC, DESC, for
ascending and descending, respectively. <sorttype> is however an optional
parameter and may be omitted, in which case, the default ASC is implied. Words
ASC and DESC are case sensitive.
5. The rest of the parameters specify skip words, which means: If they are in the input,
they must be skipped and therefore not processed. Note your input file may contain
any words including ASC and DESC and therefore they must not be confused with
the <sorttype> in the above.
6. At the end, the sorted array is printed to the standard output (stdout).
Here is what your program does.
1. Your program reads up to <n> words from the file into an array of strings. It skips
any words that match the <skipword>s. It sorts them, and prints them into the
output.
2. Your program ignores all non-words characters and threats them as whitespaces. For
instance, symbols such as underscore (_), semicolon (;), apostrophe (‘), parentheses,
etc. will all be skipped.
3. The <wtype>, to be exact, specifies what should be treated as a word character and
what as white spaces. For instance, WORD means all non-alpha characters must be
treated as whitespace. Examples are numerals: 0, 1, 2, …
4. When printing the output, separate words using a single whitespace, except for
every 10th word which must follow a single newline character.
W1 W2 W3 W4 W5 W6 W7 W8 W9 W10<newline>
W11 W12 …
7. If you haven’t done much programming with files and directories, it isn’t especially
difficult. That said, the facilities for doing so in C are somewhat more primitive than
those in languages such as Java. First, you want to make use of the functions
associated with <stdio.h>. As we discussed in class, these are the basic IO functions,
and can be applied not only to the screen and keyboard, but to disk files as well
(There are countless online examples of basic C-based I/O). Here, you will use
functions for opening and closing files, reading and writing bytes/chars to and from
files, and for checking to see if you have reached the end of the file.
2
8. <stdlib.h> provides a sorting function called qsort, that can be used to sort arbitrary
items. To use qsort, you must simply provide a comparison function that qsort can
use (Java uses a similar logic for sorting objects).
EVALUATION: Please note that the marker will be using a standard Linux system – either
Window’s WSL/Ubuntu or Docker’s Debian. Your code MUST compile and run from the Linux
command line. To evaluate the submissions, the marker will simply create a small directory
structure with a handful of files, some of which will have certain test strings inside. Every
student will be tested on the same folder structure. The marker will also ensure that you have
used good programming practices (e.g., constants, headers, basic error handling). Moreover,
there should be NO memory leaks in your program. If you dynamically create any
data/variables, you MUST clean them up before the program ends.
DELIVERABLES: Your submission should have multiple source files. To begin it will have a
file called ssort.c that will contain the main() function. This file may also contain
functions that process the command line argument.
Your submission should include a source file called fileread.c that contains the file read and
skip logic. A third file called wordtype.c will contain any logic associated with the word
types. Finally, you will have a file called output.c that contains any code related to preparing
and printing the final output, including printing error messages. Note that these files will each
be relatively small, but this format makes it much easier for the grader to see what is going on.
IMPORTANT: All files (.c and .h) must be prepared in the same folder so that they can be
compiled from the command line using the following simple gcc command
gcc –Wall ssort.c fileread.c wordtype.c output.c
Before submitting, you must test your code with this command to make sure that it works (this
particular command will produce a compiled executable called a.out). Once you are ready to
submit, compress all .c/.h files (and ONLY these files) into a zip file. The name of the zip file
will consist of "a1" + last name + first name + student ID + ".zip", using the underscore character
"_" as the separator. For example, if your name is John Smith and your ID is "123456", then your
zip file would be combined into a file called a1_Smith_John_123456.zip". The final zip file will
be submitted through the course web site on Moodle. You simply upload the file using the link
on the assignment web page.
Please note that it is your responsibility to check that the proper files have been uploaded. No
additional or updated files will be accepted after the deadlines. You can not say that you
accidentally submitted an “early version” to Moodle. You are graded only on what you upload.
Good Luck