Programming in C autumn 2009


Exercise 5

Tell in moodle in the course area (C-ohjelmointi) the name of the project work at the latest Oct. 12th. If you do you project in a group, then one of the group members should inform the name of the project work and the names of members. The group may have 2-3 students. Signing up for the first time in the course moodle area you need the word htyo.

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

Oct. 12th 2009

  1. Make an implementation of a module related to your project work, either alone or with your group. For example, a data structure you need for your work.

  2. Write a main program that allows test your module.

    Return the tasks 1 and 2 to Moodle. Of these you get a total of up to two points. About these tasks you get feedback during following week from other students.

  3. Write a function sumave, which has variable-length argument list. The first parameter tells how many double values will follow. The function will reserve space for struct where is returned the sum and the average of the given double values. The function will return a pointer to this struct.

  4. (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

  5. (12-3) Write a data type Elem to store either an integer or a character (but not both). Then, write a program which reads values from standard input and when it finds integers, stores them ia the array of Elem's. Should the reading of an integer fail, the program will read a sigle character, store it, and continue processing. Reading should terminate when either end-of-file has encountered, or 100 items have been read. Your program should output in inverse order all integers which have been stored. For the input
    12 b3 6g
    
    array will store
    12
    b
    3
    6
    g
    
    and the output will be:
    6 3 12
    
  6. Write a function min, which takes three parameters, two double values x and y and a double function f(double), and returns the smallest of the two values: f(x) ja f(y). Then, write a function min1, that has four parameters. This function is similar to min except it returns the value (the smallest of the two values: f(x) ja f(y) ) through the fourth parameter. Test your functions carefully.