L-43 MCS 275 Wed 25 Apr 2001

Review Questions on Chapter 12 and 13

  1. Define a structure to hold a person's name and height in meters and centimeters. If Bob Smith is 1m 83cm tall, we store in the structure the string "Bob Smith", and the numbers 1 and 83.

    1. Give the C code to define the structure, and use typedef to name it height.

    2. Give the instruction(s) to initialize the variable bobheight of type height with the data in the example above.

    3. Define a linked list HEIGHTS modifying the structure height and using typedef.

  2. Consider the definitions
       typedef struct point POINT;                   typedef struct list LIST;
       struct point                                  struct list
       {                                             {
          double x,y;                                   POINT  p;
          char   *label;                                LIST   *tail;
       };                                            };
    

    1. Write the C code to insert a point to the front of a given list, with prototype: LIST *insert ( LIST *l, POINT p ), where insert returns the pointer to the new list.

    2. Give prototype and implementation of a function to append a point to the end of a list. Write a small interactive program to illustrate how to use this append function.

    3. The function LIST *insert ( LIST *first, LIST *second, POINT p ) takes as argument two pointers to consecutive elements in a list, i.e.: first->tail == second. The function inserts the point p between first and second and returns the new value for first->tail.

    4. Give the C code to count the number of points, using the prototype int Count ( LIST *l ). Write two separate versions, once recursively and iteratively.

    5. The function POINT LeftMost ( LIST *l ) returns the point in the list l with the smallest x coordinate. Give the iterative and recursive implementation.

    6. Define the iterative and recursive version of void Print_Labels ( LIST *l ) that prints the labels of all points, all on separate lines.

    7. The function char *Find_Label ( LIST *l, double x, double y ) returns the label of the point with coordinates x and y. In case the point does not occur in the list, the empty string must be returned. Give the iterative and recursive implementation.

    8. int Position ( LIST *l, POINT p ) returns the position of the point in the list. If the point does not occur in the list, then -1 is returned. Give the iterative and recursive implementation. You may have to change the prototype for the recursive version.

    9. To give a new label to a point with coordinates x and y, we can use the function int Relabel ( LIST *l, double x, double y, char *label ), which returns the position of the point in the list if it occurs, and -1 otherwise.

    10. The function LIST *delete ( LIST *first, LIST *second ) takes as argument two pointers to consecutive elements in a list, i.e.: first->tail == second. The function deletes the element pointed to by second and returns the new value for first->tail.

    11. Give the implementation of void delete ( LIST *l ) that deallocates all memory occupied by the list.

  3. Write a program that can be called from the command line like prompt> create filename when the executable has the name "create". If there is not already a file with the name filename, then it will be created, otherwise the user must give permission to overwrite the existing file. If permitted (or if the file did not exist already), write your name in the file in a field of 30 characters wide, right adjusted.

  4. For a name given in the format lastname, firstname (for example "Smith, Bob"), give ONE scanf command to read in the string name the following data

    1. the entire name;

    2. the last name only;

    3. the first name only.

  5. Suppose a file contains salaries in the format
             Smith, Bob                     34023.23
             Little, Ann                    56123.32
         
    where the names occupy a field of 30 characters wide and salaries are floats written with field width 8, precision 2. In the questions below you may assume the file has been appropriately opened for the necessary operations.

    1. The function Salaries ( FILE *sal ) displays all salaries on screen. Give its implementation.

    2. Define char *BestPaid ( FILE *sal ) that returns the name of the person with the highest salary.

    3. Define FILE *TopSal ( FILE *sal, char *name, float s ) that creates a new file with the given name name containing only those names and salaries of persons with a salary higher than s. The file pointer to this new file is returned.

    4. The function void Split ( FILE *sal, float s ) splits the data in the file in two blocks: first it lists all names and salaries strictly less than s, followed by those larger than or equal to s. All data needs to be written to the original file where sal is pointing to.

    5. With int Change ( FILE *sal, char *name, float newsal ) we change the salary of the person with given name to newsal. If name does not occur in the file, then -1 is returned, otherwise 0 is returned.

    6. Write a function void PayRaise ( FILE *sal, float percent ) that raises the salaries in the given file with percent. You may assume the raise is never that high it exceeds the field width of 8 characters.

FINAL EXAM in LC C3 on Monday 30 April 2001 at 1:00-3:00PM.