Sample Questions from Midterm 1 material

Sample Questions from Midterm 2 material

Other sample questions:

1. Draw a picture showing the contents of the variables from the code below
   after the code executes.  Show pointers as arrows:

     int P = 101;
     int *Q = &P;
     int *R = Q;
     float S = 24.3;
     float T = 23.1;
     float **U;
     float *V = &T;
     float *W = (float *) R;

     Q = R;
     U = &W;

2. If A is an array variable and x is an integer variable, how are the
   expressions A+x, A[x], &(A[x]), and *(A+x) related?

3. Define the right-left rule.  How does it apply to the declaration

     float (* A) [5];

4. Explain how the malloc and free operations work.  Give an example showing
   both.

5. How would you go about dynamically allocating a two dimensional array
   with 5 rows and 8 columns?

6. Write a piece of code to read in a name in the format:

     LASTNAME, FIRSTNAME I.

     and then print that name out in the format

     FIRSTNAME I LASTNAME

     using at most 10 characters for the first name and 10 for the last name.

     You may assume that the input will always be typed correctly but you
     may not assume that the last or first names hav 10 or fewer characters.
     Also, you should make sure to read in the comma and period in the name
     when entered but they should not be included in the output.

7. Explain how the following string operations work:

     fgets
     fputs
     sprintf
     strlen
     strcmp, strncmp
     strcpy, strncpy
     strcat, strncat
     strchr, strrchr
     strstr
     strtok

8. Tori wants to write a program to keep track of all her classes.  For
   each class she wants to keep track of the following things:

     * The last name of the instructor (keeping at most 10 characters)
     * All of her scores in the class (there will be at most 20 and each
       is a whole number)
     * How many points she has in the class so far
     * How many points are possible
     * What she thinks her current grade is (represented as a single letter,
       as in A, B, C, etc.)
     * A rating of the "boring" level of the class (from 0.0 to 100.0)

     She expects that she will take at most 7 classes at any time.

     Suggest an appropriate data structure for this program.  Make sure to
     use defined constants where possible to make it easy to change the
     data structure.

9. Answer the previous question, but assume that Tori does not know how
   many classes she will take.  Your data structure should be able to
   deal with an arbitrary number of classes.

10. John has written a program to keep track of his books.  Each book is
    described by its title, the last name of the author, the name of the
    publisher, the year it was published, its ISBN# (an identification string
    consisting of 10 characters), how many pages it has, and what he thought
    of it.  Here is the data structure he used:

    #define SLENGTH 15
    #define ISBNLENGTH 11
    #define MAXBOOK 100

    typedef struct {
      char title[SLENGTH];
      char lastname[SLENGTH];
      char publisher[SLENGTH];
      int year;
      char ISBN[ISBNLENGTH];
      int pages;
      float rating;
    } BookStruct;

    BookStruct johns_books[MAXBOOK];
    int num_books = 0;

    - Write a function to insert a new book into John's collection.

    - Write a function to insert a new book into John's collection assuming
      that the books are maintained in sorted order by rating.

    - Write a function to remove a book from John's collection based on
      the last name of the author and the title of the book.

    - Write a function to remove a book from John's collection based on
      the last name of the author and the title of the book assuming the
      books are maintained in sorted order by rating.

    - Write a function to print out the names of all the books with a
      rating over 50.0.

    - Write a function to print out all of the book information in a
      nicely formatted table.

    - Write a function to sort the books by author's last name and if there
      are more than one book by the same author sort the books by that author
      by the title of the book.

    - How would you change the data structure above to represent the books
      as a linked list?

    Rewrite the seven previous functions using your linked list data
    structure.