CITS2002 Systems Programming  
 

Unit home

Project 1

help2002

Lecture & Workshop
recordings on LMS

Schedule

FAQ

Unit outline

C textbooks

OS textbooks

Information resources


Extra reading

Past projects

Recent feedback


Working effectively

Look after yourself!

Workshop 2: Conditions, Loops, Basic Formatting, and Standard Library Functions

BEFORE this and every workshop session, you're strongly encouraged to think how you would do it. You are not expected to have developed and tested a perfect solution before the workshop, but you should at least scratch out your approach on paper.

In this workshop we'll develop a short system utility to print a simple calendar.

We have already seen that Linux and Apple's macOS operating systems provide a standard utility, named cal, to print calendars. We'll aim to develop a utility, named mycal, that prints the same output (but initially, just for a single month).

prompt> cal July 2024 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Note that, even though a calendar is a 2-dimensional grid (suggesting that a 2-dimensional array is required), the program can easily be written with a 1-dimdensional array, or without any array at all.



From the information presented in lectures so far, we know:

  • our programs commence execution in the main() function.
  • values are stored in variables, each of which has a datatype.
  • we can test a variable's current value using conditional tests, and then execute different statements using the if and else keywords to control the flow within our programs.
  • we can execute a sequence of statements a known (bounded) number of times using for loops.
  • we can print out simple values using the printf() function.

So, before the workshop session, and before you start coding, think how you can combine this (already known) information to develop a solution.



EXTRA WORK (AFTER THE WORKSHOP)

As we do for the Labsheets, we provide some optional advanced tasks, which we term chili questions, prefixed by one or more chilis: 🌶 The more chilis, the more difficult the task.

  1. There's some required information that we haven't yet covered in lectures (information we can get from our computer's operating system). Try to identify that extra information.

    Consider the file first_day_of_month.c whose function determines the first day (Sun, Mon, ...) of a requested month. Identify the functions that are used to determine this information, and read their documentation for your system.

  2. We know that Linux utility programs can receive optional command-line arguments to specify actions beyond their default actions. In the case of the cal utility, if two (optional) arguments are provided, we are specifying the required month and year to be printed.

    Assuming we receive the additional two command-line arguments, which the C program receives as character strings, we can 'convert' their string representations of integers to integer values using the standard atoi() function.

    Extend your program to detect optional command-line arguments, and to print the requested calendar.

  3. 🌶 How could we support left-handers?

  4. 🌶🌶 Under both Linux and macOS, the standard cal program highlights the current day in reverse video using ANSI terminal escape sequences. Can you support that?

  5. 🌶 🌶 🌶 Extend your solution to replicate the actions of the system-provided cal program which can display all 12 months of the current year in a 3x4 grid:

    prompt> cal -y 2024 January February March Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 1 2 3 1 2 7 8 9 10 11 12 13 4 5 6 7 8 9 10 3 4 5 6 7 8 9 14 15 16 17 18 19 20 11 12 13 14 15 16 17 10 11 12 13 14 15 16 21 22 23 24 25 26 27 18 19 20 21 22 23 24 17 18 19 20 21 22 23 28 29 30 31 25 26 27 28 29 24 25 26 27 28 29 30 31 ....

  6. Wow! What happened in September 1752? (which you don't have to deal with!) :

    prompt> cal 9 1752 September 1752 Su Mo Tu We Th Fr Sa 1 2 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Presented by [email protected]