CS 509, Advanced Programming II

Fall 2002 - Test 1


  1. Write a test cast that demonstrates that the first assignment numbers the rows and columns correctly. Show both the spreadsheet input and the command-line entries. Explain what the expected output should be.


    The spreadsheet is

    0,0 0,1
    1,0 1,1
    

    The command-line entry is 0,1 or 1,0 or both. The expected output is the same as the command-line entry. The entries 0,0 and 1,1 aren't as good because they don't distinguish between rows and columns.

    Most people got this mostly right, but many people lost points unnecessarily by adding things the problem didn't ask for, such as checking for malformed specs.


  2. The following code should convert every lower-case character in str into an upper-case character

    for (? ; i <= str.size() ; ?)
      str[?] = std::toupper(str[?]);
    

    Complete the code by filling in the question marks.


    Once you recognize that the index i ends one past the last character in the string, everything else falls into place:

    for (int i = 1 ; i <= str.size() ; i++)
      str[i - 1] = std::toupper(str[i - 1]);
    

    A disappointingly high number of people got this one wrong. If you can't get something like this right, you're going to be in a lot of trouble in this course.


  3. Add the fewest number of curly brackets { and } possible to the following program so that it outputs Hello Wilma when run:

    #include <iostream>
    #include <string>
    
    int main()
    
      std::string name = "Fred";
    
      std::string name = "Wilma";
    
      std::cout << "Hello " << name << "\n";
    

    You should not change the program in any way other than adding curly brackets. Explain how your answer works.


    Either

    int main() {
      { std::string name = "Fred"; }
      std::string name = "Wilma";
      std::cout << "Hello " << name << "\n";
      }
    

    or

    int main() {
      std::string name = "Fred";
      { std::string name = "Wilma";
        std::cout << "Hello " << name << "\n"; }
      }
    

    will do. In the first case the first declaration is made temporary by the scope; in the second case the first declaration is temporarily superseded by the scope.

    Most people got this right. The ones that got it wrong added only the outer most pair of brackets. Unfortunately, without an extra pair of brackets somewhere, name is multiply defined and the program won't compile.


  4. Suppose we wanted to think about the comment delimiter // as being associative (it's not associative, but suppose we wanted to think about it that way). Would it be better to think about // as being right associative or left associative? Explain.


    According to Koenig and Moo (page 4) a left-associative operator uses as much of its left argument as possible and as little of its right argument as possible. By analogy, seems reasonable to assume that a right-associative operator uses as much of its right argument as possible and as little of its left argument as possible.

    The comment delimiter // is not a binary operator, so it doesn't make much sense to talk about it being associative. However, if we think of it as a binary operator, than it's pretty clear it uses as little of its left argument as possible (that is, none at all) and as much of right argument as possible (that is, to the end of the line). From this point of view, // would be right associative.

    This was an odd-ball question, and people did well enough on it (although most didn't do as well as they needed to). A couple of people even gave the wrong answer (that is, they claimed it was left associative) but got an 85 anyway because their explanations made it clear enough that they understood associativity.



This page last modified on 20 September 2002.