CS140 Homework 2

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!

  5. We will start covering lists in class on Tuesday and continue on Thursday, so you won't be able to answer the list versus array question until that time.

  1. (8 points) Write an anonymous struct for a tree and use typedef to name it "Tree". The struct should contain the following fields:

    1. common_name: a string of at most 15 characters. The string should be stored using a character array.
    2. latin_name: a string of unknown length
    3. year_discovered: an integer denoting the year in which the tree was first discovered
    4. evergreen: a boolean variable indicating whether or not the tree is an evergreen

  2. (6 points)

    1. 0x1e8368cc + 0x4 = _______________ (I want the hexadecimal result)

    2. 0xfe93ab43 + 0x1d7 = ________________ (I want the hexadecimal result)

  3. (24 points) Consider the following declarations:

           char name[35];
           char first_name[10];
           char last_name[20];
           char *min_name;
           char *new_name;
           char **parts;
           float speed;
           
    Write statements to accomplish the following tasks:

    1. copy the string pointed to by min_name to first_name. You may assume that min_name has fewer than 10 characters.
    2. print the lesser (i.e., earlier in alphabetical order) of the two strings pointed to by min_name and new_name in a left-justified field of 20 characters.
    3. print speed in a right-justified field of 8 characters with 3 digits allocated to the part after the decimal point.
    4. do a safe copy of the string in new_name to min_name
    5. print the combined length of first_name and last_name in a right-justified field of 3 digits.
    6. malloc an array of 50 string pointers and assign the address of the newly created array to parts.
    7. do a safe copy of the string in new_name to the fifth element of parts.
    8. create a string of the form "last_name, first_name" and store it in name (for full credit you must build the string in name rather than creating it separately and then copying it to name).
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  4. (15 points) Consider the following code:
         main(int argc, char *argv) {
           FILE *input_file;
           char buffer[101];
           int num_lines;
           int price
           double quantity;
           ...
          }
         
    Assume that the above program takes two command line arguments. The first argument is the name of the input file and the second argument is the number of lines in the file.

    1. Write a fragment of code to open the input file, test whether or not it was opened, and, if it failed to open, prints an error message that explains why it failed to open.
    2. Write an fscanf statement that reads the first two fields from input_file into price and quantity respectively.
    3. Write a statement that converts the second command line argument to an integer and assigns it to num_lines. You may assume that the argument is correct and choose your conversion function accordingly.
    4. Write an fgets statement that reads a line of input_file into buffer.
    5. Write a conditional statement that exits with a status code of 0 if the end of file has been reached for input_file.
    
    
    
    
    
    
    
    
    
    
    
  5. (10 points) Consider the following code:

         int x;
         char month[6];
         char *string_ptr;
         int *int_ptr;
    
         strcpy(month, "febr.");
         x = 20;
         string_ptr = month;
         int_ptr = &x;
         *int_ptr = 40;
         
    and the following memory layout:
    	                                ---------------------------
    	  0xffbea600 x:________________ |                         |
    
    	  0xffbea604 month: ___________ |                         |
    
    	  0xffbea608 __________________ |                         |
    
    	  0xffbea60c string_ptr: ______ |                         |
    
    	  0xffbea10  int_ptr: _________ |                         |
    	                                ---------------------------
         
    Fill in the memory contents for the above diagram (do it on your answer sheet). Note that each memory location is 4 bytes.

  6. (12 points) For each of the following scenarios specify whether you would use a linked list or an array and explain why.

    1. You have a sorted set of 1000 names and you want to efficiently answer user queries about whether or not certain names are in this dataset.

    2. You want to read an unknown number of lines into memory and print out the last ten (think carefully about this problem--the answer may not seem as obvious as you think).

    3. You want to read a file that contains 500 names of trees and associated information about these trees. As you read each tree's line into memory, you want to create a struct for that tree and insert that tree in alphabetical order into your data set.

  7. (25 points) Write a function named last_field that takes an IS inputstruct as a parameter and that 1) prints the last field on each line, along with that line's line number, and 2) prints the maximum of these last words, where maximum means the last of these words in alphabetical order. You should assume that the inputstruct has already been opened and is ready for reading. As an example, given the input:
         How are you
         doing today and how
         do YOU think 
         you will
         do tomorrow
         
    your function should produce the output:
         1: you
         2: how
         3: think
         4: will
         5: tomorrow
         you