CS140 Midterm Exam - October 5, 2004 -- Answers and Grading

Question 1

No real commentary necessary -- just grab p1.c and inputfile, and run it:

UNIX> p1 < inputfile
4
3
5
3
72
UNIX> 

What does it do? If the third word is a number, it prints the number. The only lines that are not numbers are the "Davis Love III", "# Comment" and "E. Power Biggs" lines.

Grading

This is 6 points. Six easy points, if you ask me. If you included extra lines (e.g, zeros or something for the non-numeric lines), you received half credit.

If you forgot newlines, you lost two points.


Question 2

The point of this question was to make you do a little code reading, and show that you understand some C basics. Again, you may run this yourself -- here's p2.c.

UNIX> p2 < inputfile
T               Character zero of "Tiger"
i               Character one of "Singh"
5               Character 2%1=0 of "5"
2               Character 3%2=1 of "72"
#               Character 4%1=0 of "#" (word 4%2=0)
9               Character 5%2=1 of "69"
e               Character 6%4=2 of "Cher" (word 6%6=0)
g               Character 7%5=2 of "Biggs" (word7%5=2)
UNIX> 

This program is a little sloppy -- i and j are always the line number (zero-indexed). You pick the i-th word (modulo the number of fields), and the j-th character (modulo the string length), and print it out with a newline.

Grading

This is 6 points -- you start with six, and lose one per incorrect line (you can't go negative). You lose one if you forgot newlines. Omitted lines are minus one.


Question 3

The point of this code (p3.c) was to crunch through some basics of dllists. You have one dllist. Odd lines are prepended. Even lines are appended. The first line number is one, not zero. Therefore, with each successive line, the list is: And the output is:

UNIX> p3 < inputfile
Cher
#
Phil
Tiger
Vijay
Davis
Sergio
E.
UNIX> 

Grading

Six points again. You lost a point for reversing the output (zero-indexing the line number). You lost two points if you stripped out the comment, since there is no code to do so.

Question 4

This was some stack and queue code. Odd lines get enqueued on the queue, and even lines get pushed onto the stack. Lines are zero-indexed. Therefore, here is the result of each input line:

So the output is:

UNIX> p4 < inputfile
Cher
#
Phil
Tiger

Vijay
Davis
Sergio
E.
UNIX> 

Grading

Six points again. You lost two points if you stripped out the comment, since there is no code to do so. You lost one point if you forgot the newline between the stack and queue.

Question 5

Given that first line of output, we know the following things:

So, the output of the program is:

UNIX> p5
0xbffff9b0 0xbffff9e0 0xbffff9e4
0 1 2 21 41 5 6 7 8 9 
0xbffff9c0
0xbffff9e0
0xbffff9b0
0x1
UNIX> 

Pictorally, here is the state of the system after executing "ipp = &ip." This is what you know from the first line of output:


The following pictures will show the next few lines of the program, and their effect on memory. From that you can generate the output:






Grading

Six points again. Three of these points were trivial -- the values of ipp, &a[0], and a[1] are unchanged throughout the program. Two points are allotted for the first line, and one for the second line. I was not liberal with partial credit.

Question 6

Ok -- a non-trivial program, but it is broken up into straightforward parts. You do not need to store anything past a line, so no malloc()'s or strdup()'s are necessary. You simply need to read each line, and do the following:

The only subletly is whether to handle a zero value of n. I said that all input files are legal, but is one with a zero value of n legal? You had to consider the problem, and either handle it in your code, or specify to me that you didn't consider such files legal.

Here is the answer (digest.c).

#include < stdio.h >
#include "fields.h"

main()
{
  IS is;
  double n, score;
  double total;
  int nfield, i;

  is = new_inputstruct(NULL);

  while (get_line(is) >= 0) {
 
    /* Check for comments */

    if (is->text1[0] != '#') {

      /* Find the field that holds n.  Note that this loop both does
         that and has n set when it is done. */

      for (nfield = 0; sscanf(is->fields[nfield], "%lf", &n) == 0; nfield++) ;

      /* Now scan in the scores and add them to the total */

      total = 0;
      for (i = nfield+1; i < is->NF; i++) {
        sscanf(is->fields[i], "%lf", &score);
        total += score;
      }
  
      /* Print out the average.  This assumes that n=0 is an illegal value */

      printf("%10.4lf", total/n);

      /* Finally, print out the name and the newline */

      for (i = 0; i < nfield; i++) printf(" %s", is->fields[i]);
      printf("\n");
    }
  }
}

Grading

Eight points: