CITS2002 Systems Programming  
 

Unit home

Final exam

help2002

Lecture & Workshop
recordings on LMS

Schedule

FAQ

C textbooks

OS textbooks

Information resources


Extra reading

Past projects

Recent feedback


Working effectively

Look after yourself!

Labsheet 4 - for the week commencing 18th September 2023

Lecture 11 introduced the concept of pointers, an often difficult topic for new programmers to comprehend. However, the concept is very important and, when used correctly, very powerful.

As always, take the time to understand the underlying concepts before attempting to implement the tasks below. Refer to the lecture notes and a book or tutorial for help.

One big piece of advice: never ignore warnings from the compiler about using pointers "without a cast" - this inevitably means you are doing something wrong.

Tasks

  1. Write a function named money that accepts a single integer parameter that represents a total number of cents, and breaks the total down into its numbers of dollars and cents. Your function should calculate the number of dollars, and the number of "left-over" cents, and provide these to its calling function through its parameters. For example, a value of 524 should be "returned" as 5 dollars and 24 cents.

    Your function should have the prototype:

    void money(int total, int *dollars, int *cents);

    and be called as:

    money(524, &dollars, &cents);

  2. Write a function named maximum_a() that accepts an integer array and the number of elements in that array as parameters, returning a pointer to the maximum integer in the array. It should have the prototype:

    int *maximum_a(int values[], int n);

    Your function should access elements of values as an array.

  3. Write a (very similar) version of the maximum_a() function that now accepts the integer array as a pointer.
    Use pointer arithmetic to complete this task.
    It should have the prototype:

    int *maximum_p(int *values, int n);

  4. Using the example strlen() and strcpy() functions of Lecture 11 as a guide, write pairs of functions to implement versions of the following standard C functions:

    1. strcat

    2. strcmp

    3. strncmp

    4. strchr

    5. strrchr

    6. 🌶 strpbrk

    7. 🌶 🌶 strstr

    Maintain two separate C source files for this task: one file for the array-based versions of each function (named arrays.c), and one file for the pointer-based versions (named pointers.c). Develop two separate header files, one containing the prototypes of the array-based functions (named arrays.h), and the other containing the prototypes of the pointer-based functions (named pointers.h).

    Create a single testing program named stringtest.c, containing the only main() function for this question, that tests each of the functions you write. The file stringtest.c should include both of your header files.

    Approach the writing of each function by following these steps:

    1. Firstly, read the appropriate online Linux manual entry for each function (e.g. run man strcat). Pay particular attention to the parameters of the function and its return value. Note that the online documentation will frequently use the (as yet not explained) keywords const and restrict. For now, simply ignore these and do not reproduce them in your functions (ask a demonstrator if interested in what they're for).

    2. Next, develop a version of each function using arrays (not pointers), in arrays.c, As an example, name your first function strcat_a().

    3. Write a short test function in stringtest.c file that calls your new function several times to test its execution. Depending on the function being tested, suitable parameters to the function will include simple short strings, long strings, empty strings, identical strings (when comparing them), very different strings, strings differing in only a single character, and strings containing, or otherwise, a required character (for strchr).

    4. Next, develop a version of each function using pointers (not arrays) in pointers.c. As an example, name your first function strcat_p(). Undertake the same tests on your pointer-based functions as you did on your array-based ones.


  5. A concordance is a list of significant words appearing in a body of text. The word list only includes "true" words, consisting of alphabetic characters, after all whitespace and punctuation have been discarded. You can generate a simple concordance using the command-line (shell) sequence:

    prompt> tr -c 'A-Za-z' '\n' < textfile | sort -u

    Write a C11 filter program named concordance which produces a concordance of a body of text, read from a textfile.

    Initially assume that the textfile has, say, at most 20,000 distinct words. Rather than using a two dimensional array to hold the words, we'll dynamically allocate space for each word using the standard function strdup().

    The strdup() function is not part of the C99 or C11 standards (but is, finally, defined in the new C23). However, it has been defined in the POSIX standards for a long time, and the preferred way to declare strdup() is to use this line before you #include the standard header file <string.h> :

    #define _POSIX_C_SOURCE 200809L

    Test your solution with some different text files:
    debugging.txt,   classroom.txt,   unix-1969-1971.txt,   autonomous-vehicles.txt,   baseball.txt.

    Remember that each word should only be stored once (even if repeated in the textfile).

  6. 🌶 Extend your concordance program to also count and print the frequency with which each word appears. For example, the textfile baseball.txt contains the word 'base' 18 times, and the word 'ball' 31 times. This is akin to the command-line sequence:

    prompt> tr -c 'A-Za-z' '\n' < textfile | sort | uniq -c


  7. 🌶 🌶 Write a filter program named reverse, which prints out the lines of a text file in reverse order - the last line is printed first, and the first line is printed last.
    Ensure that your program can operate on files named on the command-line, and from text lines arriving via stdin.


The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Presented by [email protected]