Week 3 Laboratory — Command Line Arguments & Makefiles

We have created some scripts that can automatically run your program against some tests. To run these tests you can execute the dry run program with an argument that corresponds to the lab and week, i.e. lab03 for this week. It expects to find all the programs to be submitted as part of this lab in the current directory. You can use dry run as follows:

prompt$ ~cs1921/bin/dryrun lab03

or specific tests (e.g. test #2) as follows:

prompt$ ~cs1921/bin/dryrun lab03 2

Exercises

  1. Write a program called concat.c that outputs, in separate lines,

    Examples of this program executing are:

    prompt$ ./concat Test1 Test2 Test3
    Name of the program: ./concat
    Number of arguments: 3
    Test1Test2Test3
    

    prompt$ ./concat c o m p 19 21
    Name of the program: ./concat
    Number of arguments: 6
    comp1921
    

    prompt$ ./concat comp1921
    Name of the program: ./concat
    Number of arguments: 1
    comp1921
    

    prompt$ ./concat 1 2 3 4 5 6 7 8 9
    Name of the program: ./concat
    Number of arguments: 9
    123456789
    

    prompt$ ./concat
    Name of the program: ./concat
    Number of arguments: 0
    
    

    Note: You should

  2. Write a program called add3.c that sums precisely 3 numerical arguments from the command line:

    Examples of 'usages' that result in correct behaviour are the following:

    prompt$ ./add3 1 2 3
    6
    

    prompt$ ./add3 9678 12505 342198
    364381
    

    prompt$ ./add3 0 0 0
    0
    

    prompt$ ./add3 -1 -2 -3
    -6
    

    prompt$ ./add3 1.0 2.0 3.0
    6
    

    prompt$ ./add3 -1.0 -2.0 3
    0
    

    prompt$ ./add3 1.9 1 1
    3
    

    Examples of usages that generate error messages are the following:

    prompt$ ./add3
    ./add3: incorrect number of args
    

    prompt$ ./add3 1
    ./add3: incorrect number of args
    

    prompt$ ./add3 1 2
    ./add3: incorrect number of args
    

    prompt$ ./add3 1 2 3 4
    ./add3: incorrect number of args
    

    prompt$ ./add3 1 2 a
    ./add3: found non-numerical arg
    

  3. Write a program called sumnum.c that sums the arguments on the command line and prints the result on stdout. If there are no arguments, the program generates no output.

  4. Write a program called countplus.c that prints on stdout a sequence of numbers from 0 to a non-negative number given on the command line. The numbers in the sequence are separated by commas. Examples of the program executing are:

    prompt$ ./countplus 1
    0,1
    

    prompt$ ./countplus 10
    0,1,2,3,4,5,6,7,8,9,10
    

    prompt$ ./countplus 0
    0
    

    You may assume that the argument is a non-negative integer, and the output appears on one line.

    You should extend the previous Makefile to build the executable.
    Make you program more robust by handling the following exceptions: if there are no arguments on the command line, or there is more than 1 argument, then a 'usage' message should be output. For example,

    prompt$ ./countplus
    Usage: ./countplus number
    

    prompt$ ./countplus 1 2
    Usage: ./countplus number
    

    The output of your program should match the above exactly.

For this lab, you should submit your files using the following give command (but the command may not work until the lab itself):


  give  cs1921  lab03  concat.c  add3.c  Makefile  sumnum.c  countplus.c

Make sure you spell the filenames correctly. The Makefile should contain rules to build the executables sumnum.c and countplus.c.