Programming in C autumn 2010


Exercise 6

To the exam you may bring one A4 paper, where you have made your own notes. You can use both sides of the paper (handwritten or printed). You should return the paper with you answer sheets after exam.

Return the module of your project work at the latest Oct. 8th. The course page will have guidance about feedback. You should give your feedback by Oct. 15th.

The project is recommended to be returned by MONDAY 1, NOVEMBER and it should be returned at latest on MONDAY 8, NOVEMBER 2010.

Oct. 14 2010

  1. (9-1) Implement an int function charInString ()   that takes three parameters, a string s an integer position pos, and an integer value value. This function returns 1 if pos is a valid position in the string s, and returns 0 otherwise. If succesful, charInString () returns the character position pos in the string s, through the parameter value.

  2. (9-5) 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.

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

  5. (9-14) Write a program that takes a command line argument representing a number in Roman notation (for example, MXLIV) and outputs this number in standard notation. Consider only values less than 1000.