Data Structures
Answer Key -- first midterm, 2004Mar16

There are 24 questions and a total of 100 points. The multiple choice questions count 3 points each. Circle the letter of your choice.

Multiple choice Questions 1 - 15: The correct answer is indicated on your returned exam.

For the five next questions, consider this makefile:

 1 it: it.o thing.o
 2	g++ it.o thing.o -R/opt/gcc-3.2/lib 
 3
 4 thing.o: thing.C thing.h 
 5 g++ thing.C -c
 6
 7 it.o: it.C thing.h
 8	g++ it.C -c
 9
10 clean: 
11	rm -rf *.o a.out it

  1. (3 points) What kind of file does "make it" produce? What is the name of the file?

    An executable named a.out
    

  2. (3 points) Modify the makefile so that the name of the file produced by "make it" is "that". You may just write the line(s) that you change. Include the line numbers.

     2	g++ it.o thing.o -R/opt/gcc-3.2/lib -o that 
    

  3. (3 points) What does "make thing" do?

    Causes an Error,  
    
    because there is no rule to make target `thing'
    
    Actual behaviour:  From default rules it tries to make thing
    by linking relocatable thing.o into an executable.  Then the error 
    message is "Undefined symbol main".
    
    

  4. (3 points) What does "make it.o" do?
    Compiles it.C (which presumably includes thing.h) to make relocatable
    code file it.o
    

  5. (3 points) If the commands "make clean; make" are entered, what happens? For each event in order, write first the line number of the command being executed and then a brief description of what it makes, starting thusly:

     11 - any previously compiled .o files are removed as are go and a.out if they exist
      8 - compile it.o -- after examining line 1 it sees it must first build target it.o
      5 - compile thing.o -- after examining line 1 it sees it must first build thing.o
      (above two can be in either order)
      2 - call compiler to just link the thing.o and it.o into the executable
         (named `a.out' or `that')
    
    

  6. (10 points) Carefully rewrite the following program using no pointers or iterators. Include type declarations of all variables. Do not change type of A, s, or maxval.
    main()
    {   string s, maxval;
        vector<string> A;
        vector<string>::iterator j;
    
        while (cin >> s ) A.pushback( s );
    
        maxval = *(A.begin());
        for ( j = A.begin() + 1; j != A.end(); ++j )
            if ( *j > maxval ) maxval = *j;
    

    main()
    {   string s, maxval;
        vector<string> A;
        int j;                                  // vector<string>::iterator j;
    
        while (cin >> s ) A.pushback( s );
    
        maxval = A[0];                          // *(A.begin());
    
        for ( j = 1; j != A.size(); ++j )       // for ( j = A.begin() + 1; j != A.end(); ++j )
            if ( A[j] > maxval ) maxval = A[j]; // if ( *j > maxval ) maxval = *j;
    

  7. (10 points) What are the arguments to the algorithm std::reverse, and what does it do? Give a general description and also a specific example.

    It is a generic function taking a start and stop iterator. it reverses
    the positions of elements in subrange of the container from start 
    (inclusive) to stop(exclusive).
    

    See SGI documentation and (for instance) text palindrome_type1 example.

  8. (10 points) As you know, we modified the Card class to provide a public Rank() member function which returns a value in 1..13, where the "honor" ranks are 1 - "ace", 11 - "jack", 12 - "queen", and 13 - "king". Complete the definition of the following function to return the honor value of a card. It returns non-honor if the rank is in 2..10.
    enum honor {non_honor, jack, queen, king, ace};
    
    honor honors( Card & c ) {
    
    // solution:
        switch (c.Rank()){
    	case 1: return ace;
    	case 11: return jack;
    	case 12: return queen;
    	case 13: return king;
    	default: return non_honor;
        }
    }
    

  9. (10 points) Define "bool palindrome_type3(string & aString)", to determine if a range contains an exact palindrome when you ignore upper/lower case distinction on letters, and ignore whitespace and punctuation. Without defining them, you you may use the generic functions copy and remove_if and the function palendrome_type2. If you use any other function, define it.

    See course palindrome code directory or text pg 127 palindrome example.