Programming in C autumn 2007


Exercise 6

To the exam you may take one A4 paper, where you have make your own notes. You can use both sides of the paper.

Oct. 12 2007

  1. (11-12) In this exercise we use a binary file "fname" storing information about employees. For each employee, store the name, the identification number and a salary:
    struct employee {
      long id;
      char name[50];
      double salary;
    };
    
    Write the following functions:
    1. int add (fname, empId, stringName, salary), where fname is a string representing a binary file empId is an integer, stringname is a string, and a salary is a double. This function appends a new employee to the binary file (an empId is akey and uniquely identifies an employee; therefore this function may fail)
    2. void moreDollars (fname, empId, incr), with three parameters: a string fname, an integer empId, and a double incr. This function operates on a binary file fname, and it increases by incr the salary of every employee whode id is greater than or equal to empId.
    3. void show (const char *fname) that shows all the information stored in the binary file fname

  2. Write a program, which reads the grades etc. of a student from a file to an array. The file is given as a command line parameter. Each row has a name of a course, credits, a grade and a date. The program prints information and counts the average of the points weighted by the credits. Possible grades are 1, 2, 3, 4 and 5 and there can be blanks in the name of the course. The printing order is given by a command line parameter. Give the field which is used to order the rows. Plus sign '+' is used to obtain ascending order and '-' sign for descending order. For example program studies +3 , should print the information from the file studies using ascending order of the field 3 (grades field).
    Use functions.

  3. (11-6) Write a void function input, which has two parameters: fname, which represents the name of the text file, and storage, which represents an array of pointers to characters:
    #define MAX 100
    char* storage[MAX];
    
    input() reads the file fname and stores storage lines from this file in the array. Do not make any assumptions about the length of input lines (use a function getline() from Example 10-3). The function input() should then remove all lines that are empty (contain only the end-of-line character, possibly preceded by whitespace characters.) from this array, and report the number of lines that have been removed. Then write the main function which calls input(), and then outputs the contents of the array storage to the standard output stream.

  4. (8-9) Write a function
    void compareGen(const void *block1, const void *block2, size_t elemSize,
                    size_t block1Size, size_t block2Size,
                  int (*compareIt) (const void*, const void*));
    
    which performs a lexicographical comparison of the two blocks. Test your program using blocks of doubles and blocks of pointers to doubles.

  5. (8-11) Implement a generic module Bags, that represents an unordered collection (a bag; duplicates allowed). There should be operations to add an element to the bag, and remove it from the bag. Add enumerations over the collection. Test your module with a collection of double objects, and a collection of integer objects.