CSc 352: Fall 2002: Placement Exam


Date: Thu Aug 29 2002.
Due: 12:00 Noon, Thu Sept 5 2002.

Submitting your code: Turn in your files on host lectura.cs.arizona.edu using the command

turnin cs352_placement file1 file2 ... filen

Please follow the directions below carefully and exactly: understanding and programming to a specification is an important aspect of this exam.


  1. Write a C program that behaves as follows:

    Input :
    A file containing M rows (M >= 0), each row containing N integers (N >= 0) separated by whitespace (i.e., some number of blanks and/or tabs).
    Output :
    1. each row of the original input file, preceded by the sum total of the integers in that row; and
    2. a final row whose first element is the sum of all the individual row sums, and whose next N elements are the sums of the individual columns of the input file.
    Thus, your output should have a total of M+1 rows, with each row containing N+1 columns.

    Example: Given the input

    110-37125
    31769-2
    011912313
    your program should produce the output
    -9 110-37125
    33 31769-2
    156 011912313
    180438-2214416
    where the entries in boldface indicate the totals computed by your program.

    In the output you generate, it is not necessary to retain the same whitespace between numbers within a row as in the input file.

    Error Conditions :
    1. The program must be invoked with a single argument, which is the name of a (readable) file.
    2. All the rows in the input file should have the same number of entries, and each entry should be an integer.

    If any of these conditions is violated, your program should give an error message identifying the problem as far as possible and exit with status -1. For example, the error message produced if the file specified does not exist should be different from that produced if the file exists but is not readable (these are just two examples of things that can go wrong; other such situations should be dealt with similarly).

    Restrictions :
    1. Your program should use an amount of memory proportional to the number of data elements in the input.
    2. There should not be any a priori hard limits on the maximum number of rows and/or columns in an input file.

    In other words, it is not acceptable to allocate a large fixed array of integers to hold the input data.

    Invoking your Program :
    Your (executable) program will be called mytotal and will be invoked as
    mytotal infile

    Files to Turn in :
    Turn in (a) all source files for your program, and (b) a Makefile that supports the following targets:

    make mytotal: causes your program to be built without any errors or compiler warnings.

    make clean: deletes all binary files (*.o and mytotal) in the directory.

    Exit Status :
    The program should exit with status 0 if execution completes normally, -1 if an error was encountered (see above).

  2. Write a shell script named mycompare (for a shell of your choice, provided that the script invokes the appropriate shell itself), that is intended to compare student grades in two classes and identify students who did well in one class but poorly in the other. Your script should behave as described below:

    Invocation :
    The script will be invoked with two arguments, i.e., as
    mycompare file1 file2
    Each of file1 and file2 must name a file that exists and is readable.

    If invoked with an incorrect number of arguments, or if any of the files specified does not exist or is not readable, the script should give a descriptive error message and exit with status -1.

    Input File Format :

    Each input file will consist of a number of lines, each line containing a number and a letter (one of A, B, C, D, E), e.g.:
    289383 B
    692777 A
    747793 A
    885386 C
    516649 B
    202362 C
    368690 E
    897763 B
    The number represents a student ID, and the letter is the student's grade in the class represented by that file.

    You may make the simplifying assumption that the student ID is separated from the grade by some number of blank (i.e., space) characters, but not tabs or any other whitespace character. However, do not make any assumption about the number of space characters separating the two fields in any line.

    You may also assume that the input files, if they exist and are readable, are in the correct format (as described above), so you need not check the format of the file for correctness.

    Behavior :
    When invoked as mycompare file1 file2, the script should print out each student id that satisfies the following property:
    The student received an A or B ("did well") in one of the two courses specified by the input files, but received a D or E ("did poorly") in the other course.
    Exit Status :
    The script should exit with status 0 if execution completes normally, -1 if an error was encountered (see above).