Programming in C autumn 2009


Exercise 6

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

Return the module of your project work at the latest Oct. 12th. The course page will have guidance about feedback and to whom to give feedback on 14.10. You should give your feedback by Oct. 21st.

Project work should be returned by no later than Nov. 9th.

Oct. 19 2009

  1. Write a function void fill (char *p, int n, char pat),   to initialize n bytes of memory with the value pat, starting at address p.

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

  3. (8-10) Write a function
    void PrintGen(const void *block, size_t elemSize, size_t blockSize,
                  void (*printIt) (const void*));
    
    which prints all elements in the block using the "callback" function printIt. Use the "Traversing a Block of Objects" idiom. Test your program using a block of doubles, and a block of pointers to doubles.

  4. (9-12) Write a program that takes command line arguments. There are two possible arguments
    program file1 -d
    
    or
    program file1
    
    In the first case, this program will display on the standars output the first d lines from the file file1. In the second case, it will display up to the first 20 lines from this file. If anything goes wrong (for example, the file file1 does not exist, or d is not an integer value), an error message should be displayed.