CS140: Homework 6

  1. Consider the following code:
    	int *int_array = (int *)malloc(sizeof(int) * 40);
    	
    Write a loop that uses scanf to read 40 integers from stdin directly into the elements of the integer array. In other words, you cannot pass an integer variable to scanf and then assign the integer variable to the appropriate element in the array. That requires an intermediate step and is less efficient than a direct read.

  2. Consider the following code:

         char word[7];
         char *string_ptr;
         int *double_string_ptr;
         int i;
    
         (1) string_ptr = (char *)malloc(sizeof(char) * 50);
         (2) double_string_ptr = (char **)malloc(sizeof(char *) * 10);
         for (i = 0; i < 10; i++) 
    	double_string_ptr[i] = string_ptr + (i * 5);
         
         
    Further suppose that you are given the following information (all addresses are in base 10):
         address of word = 1000
         address of string_ptr = 1008
         address of double_string_ptr = 1012
         address returned by malloc in statement (1) = 5000
         address returned by malloc in statement (2) = 6000
         sizeof(char) = 1
         sizeof(char *) = 4
         

    1. What is the address of word[4]?
    2. What is the value of string_ptr?
    3. What is the value of double_string_ptr?
    4. What is the value of &string_ptr[6]?
    5. What is the value of &double_string_ptr[6]?
    6. What is the value of double_string_ptr[4]?

  3. Draw how one of Dr. Plank's dllists will appear after the following sequence of operations is performed:
    	Dllist my_list = new_dllist();
    	Dllist one_node = dll_append(my_list, new_jval_i(1));
    	Dllist two_node = dll_prepend(my_list, new_jval_i(2));
    	Dllist three_node = dll_insert_a(two_node, new_jval_i(3));
            Dllist four_node = dll_insert_b(two_node, new_jval_i(4));
    	

  4. Write the code question from the first midterm using Dr. Plank's dllist library. Here is a slightly re-written version of the question:

    You are to write a function that given two sorted lists of integers, L1 and L2, prints the integers that are common to both lists. For example, if L1 contains the integers 1, 3, 6, 10, 12 and L2 contains the integers 2, 3, 10, 12, 15 then your program should print:

    	3
    	10
    	12
    	
    You should assume that the lists are stored as Dllists and that the integers are stored in Jvals in the Dllists. The function's prototype is:
    	void print_common_elements(Dllist L1, Dllist L2);
    	

  5. Write a function called reverse that takes a Dllist as input and returns a new Dllist with the elements of the first Dllist reversed. The first Dllist should be left unaltered. Note that you will have to copy the value fields of nodes in the input list to nodes in the reversed list. Since the value fields are Jvals you can simply pass them to the appropriate function and they will be copied. The function's prototype is:
    	Dllist reverse(Dllist input_list);