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!

Labsheet 2 - for the week commencing 5th August 2024

Many tasks will require you to write functions that take one or more arguments and perform operation on them. Typically, these arguments will be read from the command-line of the program. The name of your program may be obtained from the variable (array element) argv[0]. The first "true" command-line argument to your program may be obtained from the variable (array element) argv[1]. Subsequent command-line arguments may be obtained with the code argv[2], argv[3], and so on. Note that all command-line arguments are strings.

Some of this week's tasks require your programs to accept command-line arguments and convert them to integers. The following code will do this conversion for you:

#include <stdlib.h> ..... // Determine the numerical value of the program's 1st command-line argument     int value = atoi( argv[1] );

The sample program square.c demonstates this concept - after checking that the program is given the correct number of command-line arguments, the program converts each argument to an integer, and passes each integer value to a function. The standard C function atoi(), pronounced a-to-i, converts an alphanumeric character string to an integer.

When displaying output to the screen, note that the last line of all output must end with a newline character, otherwise the line will be over-written by the shell's next command prompt. A newline character is represented by the 2-character sequence \n, and requests that the output device (e.g. your screen) "go to the next line".

Tasks

  1. A year is a leap year if it is divisible by 400, or if it is divisible by 4 and not divisible by 100. For example - 1996, 2000, and 2012 were leap years, while 1899, 1900, and 2013 were not. Write a program that prints whether a year, supplied as a command-line argument, is a leap year or not.

  2. Incorporate your support for leap years from the previous task, into your solution to the calendar printing program from Workshop-2.

  3. Write a program which accepts exactly 3 command-line arguments (other than the program's name in argv[0]), converts each to an integer value, and prints the maximum of the 3 values.

  4. Rewrite your solution to the previous task so that it now prints the maximum of an arbitrary number of command-line arguments, perhaps 3, or 10, or 20....

  5. The Luhn algorithm was developed by German computer scientist Hans Peter Luhn in 1954, and is used by many credit card companies to distinguish valid credit card numbers from random sequences of digits. To determine if a given credit card number passes the Luhn test:

    1. Consider the card's digits from right-to-left.
    2. Taking the first, third, ... and every odd digit (from right-to-left), sum them to form the sum s1
    3. Taking the second, fourth ... and every even digit (from right-to-left):
      • Multiply each even digit by two, sum that product's digits, and add together all those sums to form the sum s2
    4. If (s1 + s2) ends in zero, then the original number was a valid credit card number!

    Encourage everyone in the Lab to give you their credit card number (so you can test your code, of course).

  6. Write a program that prints out the ordinal description of the (assumed numerical) arguments supplied to the program. For example:

    prompt> ./ordinal 1 6 11 12 21 22 23 1st 6th 11th 12th 21st 22nd 23rd

  7. Write programs to print each of the following shapes. Use C's for loops to control your printing, rather than just 'fixed' calls to printf (imagine if a command-line argument indicated how big the Christmas tree should be!):
    a. *       
       **      
       ***     
       ****    
       *****   
    
    b.     *   
          **   
         ***   
        ****   
       *****   
    
    c. *****   
       ****    
       ***     
       **      
       *       
    
    d.     *
          ***
         *****
        *******
       *********
    

  8. Ackermann's function is a function that's simple to implement, but very difficult to understand. Ackermann's function is noted for its need for deep recursion to calculate values for even small parameter values. While it was once used to benchmark processor speeds, today it only keeps Mathematicians awake at night.
    • Trivially implement Ackermann's function.
    • Extend your implementation to count the number of recursive calls and the maximum recursive depth required for, say, A(1, 2) and A(3, 3).
    • 🌶 🌶 Redesign your implementation so that it doesn't require any global variables to maintain the counts of recursive calls and maximum recursive depth.

  9. 🌶 Write a program that prints out the "byte size" description of the (assumed numerical) arguments supplied to the program. For example:

    prompt> ./bytesize 1 1200 2444555 5666777888 1Byte 1KByte 2MByte 5GByte (this one may be a challenge!)

  10. 🌶 🌶 Write a program that prints out all "Roman numeral" equivalents of the numbers between 1 and the single argument supplied to the program. Use the rules for writing Roman numerals, and the online calculator, from: Calculator Soup (just for the numerals I, V, X, L, C, D, and M).

  11. 🌶 🌶 🌶
    Pursue impossible (?), or don't waste your time
    Consider the following self describing sentence (which may or not be a correct solution to this problem):

    This sentence has forty three As, sixty eight Bs, ..... and seventeen Zs.

    Apparently, these things are named pangrammatic autograms. Write a program to print out an autogram (alphabetic case is ignored when counting the letters).

The University of Western Australia

Computer Science and Software Engineering

CRICOS Code: 00126G
Presented by [email protected]