CS140: Homework 7

  1. Suppose you have a program that uses the following files:

    1. The source files you wrote are in maze.c, maze-helper.c, and list.c.
    2. The include files are maze.h, which is found in a directory named /home/bvz/maze/include, and fields.h, which is found in /home/bvz/libraries/include
    3. The object file for fields.h can be found in /home/bvz/libraries/objs

    Write the command that compiles this program into an executable named maze and makes it suitable for debugging with gdb.

  2. Write a short program that simulates pushing and popping functions from a stack. Your input consists of lines from standard input that begin with either the keyword "call" or "return". If a line starts with "call" it also has a second field which is the name of a function. If a line starts with return it has an optional second field which is the name of a function. You are to write a program that uses the fields library to read these lines. Your program perform the following actions:

    The following example shows some sample input and what the stack would look like after each line is processed. Of course the function names would be pointed to by character strings and the actual values on the stack would be Jvals that pointed to the character strings.

    	call a          // stack = a
    	call b		// stack = b a
    	call a		// stack = a b a  (a was pushed onto the front)
    	return		// stack = b a    (b was popped)
    	call c		// stack = c b a
    	call d		// stack = d c b a
    	call a		// stack = a d c b a
    	return c	// stack = c b a (a and d got popped)
    	
    Do not worry about printing anything, checking for error conditions, or compiling/executing your code.

    Assume that you are using a pre-defined stack library that has the following interface:

    1. void push(void *value, void *stack): push the given value onto stack
    2. void *pop(void *stack): pop the top element off the stack and return its value. Remember to delete the node holding the top element. If the stack is empty return 0.
    3. void *top(void *stack): return the value of the top element on the stack but do not pop it.
    4. void *create_stack(): create a record for a stack and return it as a void *.
    5. bool is_empty(void *stack): return true if the stack is empty and false otherwise.

  3.  
         a. array
         b. singly-linked list
         c. doubly-linked list
         d. queue
         e. stack
    

    For each of the following questions choose the best answer from the above list. Sometimes it may seem as though two or more choices would be equally good. In those cases think about the operations that the data structures support and choose the data structure whose operations are best suited for the problem. You may have to use the same answer for more than one question:

    1. ____________ The most space-efficient data structure you could use to implement a stack in which there is an upper limit, max, on the number of elements that can be stored on the stack.
    2. ____________ The most space-efficient data structure you could use to implement a stack in which the the number of elements that can be stored on the stack is unlimited.
    3. ____________ The data structure which allows any of its elements to be accessed in constant time.
    4. ____________ The data structure you would use if you needed to be able to insert an element anywhere in the data structure.
    5. ____________ The data structure you would use to ensure that the last item added to the data structure is always the first item removed from the data structure.
    6. ____________ The data structure that allows you to traverse its elements in either forward or reverse order.
    7. ____________ The easiest data structure you could use to implement a queue with an unbounded number of elements.
    8. ____________ The data structure you would use to ensure that items are always added to the end of the data structure and removed from the front of the data structure.

  4. Question 2.6.c in Weiss. Only answer the first 4 questions and compute both T(n) and O(n). For question 2.6.c.4 choose the most pessimistic value of i for the inner loop. Remember that in computing T(N) we want the most pessimistic estimate, not the best or average estimate of running time.
  5. Programs A and B are analyzed and found to have worst-case running times no greater than 150N log2N and N2, respectively. Answer the following questions (Note: you should not need a calculator to answer any of the following questions):

    1. Which program has the better guarantee on the running time, for large values of N (N > 10,000)?

    2. Which program has the better guarantee on the running time, for small values of N (N < 100)?

    3. Which program will run faster on average for N = 1,000?

    4. If you did not know the size of the input, which program would you prefer to use? Why?
  6. Behold the following function:
         int max_sum(int *array, int size, int *save_i, int *save_j) {
             int max = 0;
    	 int i, j;
    
    	 for (i = 0; i < size-1; i++) {
    	     for (j = i+1; j < size; j++) {
    	          if (array[j] > array[i]) {
    		      if ((array[j] + array[i]) > max) {
    		          max = array[j] + array[i];
    			  *save_i = i;
    			  *save_j = j;
    		      }
    		  }
    	     }
              }
    	  return max;
          }
          
    1. What is T(size) for the inner-most loop? You are allowed to use i in your mathematical expression for T(n). Note that size represents the size of the input for max_sum. Hint: when trying to figure out how many times the inner loop will iterate, choose the most pessimistic value of i possible. Remember that we are looking for the worst case, not the best case or even the average case.

    2. What is T(size) for max_sum as a whole?

    3. What is the big-O running time of max_sum?

    4. Suppose main has the following declarations:
            #define SIZE 10
            int a[SIZE];
            int max;
            int index1;
            int index2;
            

      Write a line of code that will call max_sum and save the return result in max, the value of save_i in index1 and the value of save_j in index2.

  7. What is Big-O of each of the following functions: