Answers to Exam 2 Fri 13 Apr 2001

    1. Use typedef to define a linked list, named LIST, to represent a list of books.

      Each book is represented by its authors (a string), its title (a string), its year of publication (an integer), and its price (a float).

              typedef struct list LIST;
              struct list
              {
                 char   *authors;
                 char   *title;
                 int    year;
                 float  price;
                 LIST   *tail;
              };
      
                                                              +--------+
                                                              |    /10 |
                                                              +--------+
      
    2. Give the sequence of C commands to create a list books of the type LIST defined in 1.1, which contains information about the textbook for this course.

      Start with the allocation and then assign the member values for the book by Kelley and Pohl, with title `C by dissection', published in 2001. Also fill in the price you paid for the book (or what you think it is worth).

               LIST *books;
               books = (LIST*)calloc(1,sizeof(LIST));
               books -> authors = "Kelley and Pohl";
               books -> title = "C by dissection";
               books -> year = 2001;
               books -> price = 23.99;
               books -> tail = NULL;
                                                              +--------+
                                                              |    /10 |
                                                              +--------+
      
  1. Consider :
                  typedef struct popsize POPSIZE;
                  struct popsize
                  {
                     char     *country;      /* name of a country */
                     int      population;    /* size of its population */
                     POPSIZE  *tail;
                  };
    
    1. The function POPSIZE *insert(POPSIZE *l, char *country, int population) creates a new structure with the given country, population, and inserts it to the front of the given list l. The new list is returned. Give the implementation :
            POPSIZE *insert(POPSIZE *l, char *country, int population)
            {
               POPSIZE *p;
      
               p = (LIST*)calloc(1,sizeof(LIST));
               p -> country = country;
               p -> population = population;
               p -> tail = l;
      
               return p;
            }
      
                                                              +--------+
                                                              |    /15 |
                                                              +--------+
      
    2. The function int highest(POPSIZE *l) returns the highest population in the list l. Write an ITERATIVE definition for this function :
            int highest(POPSIZE *l)
            {
               POPSIZE *p;
               int max = 0;
      
               for (p = l; p != NULL; p = p -> tail)
                  if (p -> population > max)
                     max = p -> population;
      
               return max;
            }
      
                                                              +--------+
                                                              |    /15 |
                                                              +--------+
      
  2. Suppose an input file contains lines with two floats separated by a comma :
        232.3434531    ,   2.2323
        0.23 ,        8.28232
    
    As you can see, the position of the comma on each line is rather arbitrary.

    Let fp be a file pointer variable, x the first, and y the second float variable.

    Using these variables, give ONE fscanf command to read x, to skip the comma, and to read the y variable from the file pointed to by fp.

        fscanf(fp,"%f%*[^,],%f", &x, &y);
    
                                                            +--------+
                                                            |    /15 |
                                                            +--------+
    
  3. The function int CountLines(FILE *fp) returns the number of lines in the file.

    Assuming that the file is already opened for reading, give the definition below :

        int CountLines(FILE *fp)
        {
           int cnt = 0;
           char c;
    
           while ((c = getc(fp)) != EOF)
              if (c == '\n') cnt++;
    
           return cnt;
        }
    
                                                            +--------+
                                                            |    /15 |
                                                            +--------+
    
  4. Suppose the file message contains the following 11 characters on the next line :

    rpshytaeepa

    To make sense of this message, execute the following program :

          #include 
    
          int main()
          {
             FILE *infile;
             char c;
             int i,p;
    
             infile = fopen("message","r");
    
             p = 0;
             for (i = 0; i < 11; i++)
             {
                p = (p + 3)%11;
                fseek(infile,p,0);
                fscanf(infile,"%c",&c);
                printf("%c",c);
             }
    
             fclose(infile);
             return 0;
          }
    
    Complete the table with values for p and c below :
       ------------------------------------------------------------
       | i |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |  8 |  9 | 10 |
       ============================================================
       | p |  3 |  6 |  9 |  1 |  4 |  7 | 10 |  2 |  5 |  8 |  0 |
       | c |  h |  a |  p |  p |  y |  e |  a |  s |  t |  e |  r |
       ------------------------------------------------------------
    
                                                            +--------+
                                                            |    /20 |
                                                            +--------+
    
    FINAL EXAM in LC C3 on Monday 30 April 2001 at 1:00-3:00PM.