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 left argument of a stream extraction has to be an input stream. Given a chain of extractions, the immediate left operand of any but the leftmost extraction is a variable, not an input stream. The left argument of any extraction in the chain has to include all the chain to the left of the extraction; otherwise it won't be an input stream.
Similarly, the right argument of a stream extraction has to be a variable. The only part of the chain an extraction can use to its right is the variable immediately following the extraction. To be useful, an extraction has to use as much of its left argument and as little of its right argument as possible, which is exactly backwards from being right associative.
A lot of people gave "because it's left associative" as an answer, which doesn't really demonstrate a firm grasp of the concept of associativity.
{
and }
possible to the
following program so that it outputs Hello Fred
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.
int main() { std::string name = "Fred"; { std::string name = "Wilma"; } std::cout << "Hello " << name << "\n"; }
The inner scope makes the second declaration non-conflicting and temporary.
I though this was a gimme, and it was.
str
into
an upper-case character
for (int i = str.size() ; ? ; ?) str[?] = std::toupper(str[?]);
Complete the code by filling in the question marks.
Everything falls into place once you recognize that i
is starting one past
the end of the string:
for (int i = str.size() ; i >= 1 ; i--) str[i - 1] = std::toupper(str[i - 1]);
Too many people got this one wrong. If you can't reason your way around a simple loop like this, you are doomed.
Show both the spreadsheet input and the command-line entries. Explain what the expected output should be.
A possible spreadsheet is
aaa ccc aaa ccc aaa bbb ccc
The element spec is 0-1,0-1
, which specifies two columns, and the section
of the spreadsheet output is
aaa aaa
which correctly only has one column.
A number of people tried to answer this question by giving a spec that had columns to the right of the spreadsheet; that is, giving 1-2,3-4 on a spreadsheet with three columns. Such spec are malformed and can only result in an error message, which is not what the problem asked for.
A few people tried to answer this question by using a column of tab characters. While this produces output that looks like it contains fewer columns than were specified, by the definition of columns given in the assignment, it should be clear that the output has as many columns as were specified, even if you can't seem them.
This page last modified on 20 September 2002.