CS140 Homework 3

Fall 2009

Instructions

  1. This homework assignment will be a perfect review document/exercise for your first midterm since it is the midterm that I gave a previous class. Hence it gives you an excellent idea of what might appear on the exam, although you shouldn't confine your studying to just the questions that appear on this homework assignment. I may well ask alternative questions, but it does give you a good idea of what to expect and what my exam style looks like. In particular I tend to ask a coding question on the exam, so be prepared for one.

  2. Prepare your answers using either a word processor or an ascii text editor, such as vi, emacs, or notepad.

  3. Print your answers and hand them in at the beginning of class on the assigned due date.

  4. You get an hour and 15 minutes for the exam, so this is the time frame you should give yourself for answering the questions on this homework assignment. I know that the coding question may take you a bit more time than that. Take the time to try to get it right and remember that there is a good chance that the question on the exam will involve the fields library, string operations, malloc, and printf, so be familiar with those concepts!
  1. (10 points) You are asked to design a struct for a game. The game has three types of objects: 1) monsters, 2) potions, and 3) players. All three objects should have a name field that is a pointer to a character string. Each object also has some unique fields:

    1. Monsters have integer fields for their strength and number of lives.
    2. Potions have a float field for the protection level that they provide and an integer field for their duration.
    3. Players have an integer field for their strength and a float field for their wealth.

    Design a struct given the above information. You may need to use a union inside your struct and you may need to define a type field to distinguish among the types of objects. The name field will not distinguish among the three types of objects--it is merely meant to name the item, such as "dragon" or "strength elixir". On the exam I will be more likely to ask you to interpret union code, rather than write union code, but this is good practice.

  2. (10 points) For each of the following operations, select the most appropriate running time from the following list:
    O(1)O(log n)O(n)O(n log n)O(n2)

    1. Pushing an item onto a stack.
    2. Appending an item to the end of a doubly linked list.
    3. Trying to determine where to insert a new word in a sorted doubly linked list.
    4. Enqueuing an item on a queue.
    5. Deleting a node from a doubly linked list when you already have a pointer to the node.

  3. (10 points) You are given the following declarations:
      void *value;
      person *p;
      
    1. Write a statement that assigns value to p.
    2. Write a statement that assigns p to value.

  4. (10 points) In class we discussed two important uses of void *'s in C code and in lab you have used void *'s in each of these two ways. Name these two uses of void *'s.

  5. (20 points) You are given the following declarations for a doubly linked list:
         struct node {
             struct node *next;
             struct node *prev;
             int value;
         };
    
         struct node *my_node;
    
    Suppose you are given the following information:
         my_node = 0xa0
         my_node->prev = 0x9c
         my_node->next = 0xb4
         my_node->value = 6
         my_node->next->next = 0xc8
         my_node->next->value = 8
         

    Draw the memory diagram for my_node and its successor node. In particular, label the boxes in the following diagram with the appropriate hexadecimal addresses and in each box either indicate the name of the field and the value stored at that memory location or leave the box blank if the contents at that memory location are unknown.

                         --------------------------------
                         |                              |
                0xa0     |                              |
                         --------------------------------
                         |                              |
                0xa4     |                              |
                         --------------------------------
                         |                              |
                0xa8     |                              |
                         --------------------------------
                         |                              |
                0xac     |                              |
                         --------------------------------
                         |                              |
                0xb0     |                              |
                         --------------------------------
                         |                              |
                0xb4     |                              |
                         --------------------------------
                         |                              |
                0xb8     |                              |
                         --------------------------------
                         |                              |
                0xbc     |                              |
                         --------------------------------
                         |                              |
                0xc0     |                              |
                         --------------------------------
                         |                              |
                0xc4     |                              |
                         --------------------------------
         

  6. (20 points) Consider the following declarations and function: push(char c, Stack *s); // push a character c onto s char pop(Stack *s); // pop a character c off s and return its value char top(Stack *s); // return the top character on s but do not pop it bool isEmpty(Stack *s); // return true if the s is empty and false otherwise void mystery(Stack *stack) { char token; while (scanf("%c", &token) != EOF) { if ((token == '+') || (token == '-')) { if (!isEmpty(stack) && top(stack) != '(') printf(" %c", pop(stack)); push(token, stack); } else if (token == '(') { push(token, stack); } else if (token == ')') { while (top(stack) != '(') { printf(" %c", pop(stack)); } pop(stack); } else printf(" %c", token); } while (!isEmpty(stack)) { printf(" %c", pop(stack)); } } Suppose the input is:
    (a+(b-c))
    
    1. What is the contents of stack after '-' is read and processed?

    2. What is the contents of stack after the first ')' is read and processed?

    3. Given this input, what does mystery print?

  7. (20 points) Write a function that reads words from an inputstruct and counts the number of times each word occurs in the file. The output of the function should be a linked list that has an entry for each word and the number of times that word occurs in the file. Do not worry about potential errors and assume that the inputstruct has already been created and is passed as a parameter. Use the sllist library to implement your solution. If your input file is:
            the quick brown fox
            jumped over the brown
            fence
            
    then your list of frequency counts might look like:
    	the 2
            quick 1
            brown 2
            fox 1
            jumped 1
            over 1
            fence 1
            
    Case does matter. For example, if the first "the" was capitalized in the above input file, then "The" and "the" would appear as two separate words. This assumption makes your life easier, not more difficult because you do not have to worry about converting words to all lower or upper case.

            Sllist *word_frequency(IS input_file) {