Question 1: 8 Points
The program is in
q1.cpp if you want to run it yourself.
- Part A: cerr goes to the screen, so the screen will print "Binky\n".
- Part B: cout goes to standard output, which has been redirected to the
file out1.txt. Therefore, out1.txt will print "LuigiThor\n".
- Part C: The command line argument is ignored. All statements go to the screen:
"LuigiBinkyThor\n\n"
- Part D: The program does nothing to out1.txt. Therefore, if it doesn't
exist, it will not exist, and if it does exist, then it will be unchanged by the program.
Grading
2 points for A and B. Three points for C. One point for D.
Question 2: 12 points
The programs are in
q2a.cpp, q2b.cpp and
q2c.cpp, and the input files are in
a.txt,
b.txt,
c.txt and
d.txt, so you can run them yourselves:
UNIX> q2a < a.txt
7 8
UNIX> q2a < b.txt
10 11 - Cin reads words -- it doesn't care about lines.
UNIX> q2a < c.txt
1 6 - The second cin failed, so j was unchanged.
UNIX> q2a < d.txt
5 6 - The first cin failed, so both i and j were unchanged.
UNIX> q2b < a.txt
7.0 8.7
UNIX> q2b < b.txt
10.0 11.0
UNIX> q2b < c.txt
1.2 4.0
UNIX> q2b < d.txt
5.9 6.0 - printf() does rounding
UNIX> q2c < a.txt
7 8.7 9.7
UNIX> q2c < b.txt
10 11 21
UNIX> q2c < c.txt
1.2 4 5
UNIX> q2c < d.txt
Tyler 3 4
UNIX>
Grading
- Part AA, First word: .5 points
- Part AA, Second word: .5 points
- Part AB, First word: .5 points
- Part AB, Second word: .5 points
- Part AC, First word: .5 points
- Part AC, Second word: .5 points
- Part AD, First word: .5 points (.3 if you said there was an error)
- Part AD, Second word: .5 points (0 if you said there was an error)
- Part BA, First word: .5 points. (.3 if you forgot the trailing .0)
- Part BA, Second word: .5 points. (.3 if you forgot the trailing .0)
- Part BB, First word: .5 points. (.3 if you forgot the trailing .0)
- Part BB, Second word: .5 points. (.3 if you forgot the trailing .0)
- Part BC, First word: .5 points
- Part BC, Second word: .5 points (.3 if you forgot the trailing .0)
- Part BD, First word: .5 points (.3 if you said there was an error)
- Part BD, Second word: .5 points (0 if you said there was an error)
- Part CA: .4/.3/.3 for the three words.
- Part CB: .4/.3/.3 for the three words.
- Part CC: .4/.3/.3 for the three words.
- Part CD: .4/.3/.3 for the three words.
You lost a point overall with multiple lines, commas, or extra decimal spaces.
Question 3: 8 points
This is a nuts-and-bolts stringstream problem. Obviously, the logic of your program
can be slightly different from mine, but only slightly. In
q3.cpp, I've included a main() that tests it by reading
lines from standard input and passing them to stoivec(), then printing the resulting
vector. The code below just implements stiovec:
vector <int> stoivec(string &s)
{
vector <int> v;
istringstream ss;
int i;
string dummy;
ss.clear(); // This line is not necessary.
ss.str(s);
while (1) {
if (ss >> i) {
v.push_back(i);
} else {
ss.clear(); // This one is necessary.
if (!(ss >> dummy)) return v;
}
}
}
|
Given the wording of the question, returning a vector of strings would have been fine too, as
long as you return only the words that convert successfully to integers.
Grading
- Prototype: 1 point
- Reference variable for the argument: 1 point
- Logic of the answer: 3 points
- Details of the answer: 3 point
In particular, if you simply did "ss >> i" without any checking, you lost 3-4 points.
Question 4: 8 points
This tests your string arithmetic, and character arithmetic.
q4.cpp has a main() with it that reads
lines from standard input, creates a vector from the words, and prints out
the return value of svectos.
string svectos(vector <string> &v)
{
string s;
int i;
for (i = 0; i < v.size(); i++) {
if (i != 0) s += " ";
s += v[i];
}
for (i = 0; i < s.size(); i++) {
if (s[i] >= 'A' && s[i] <= 'Z') s[i] += ('a' - 'A');
}
return s;
}
|
I was appalled by the number of students who used numbers like 97 for 'a'. Evidently I need to do a better
job of teaching that you should use 'a' -- it is more readable and keeps you from making mistakes, which happened
almost inevitably when you used numerical constants.
Grading
Same as question 3.
Question 5: 8 points
You should all be seasoned veterans at averaging numbers. Even though you
are averaging integers, you should use doubles to compute the averages.
q5.cpp uses the answer from question three to
create the vector of integers from each line of standard input. When it
is done reading, it calls avg():
typedef vector <int> IVec;
void avg(vector <IVec> &v)
{
double n;
double t;
int i, j;
for (i = 0; i < v.size(); i++) {
if (v[i].size() == 0) {
printf("Bad\n");
} else {
t = 0;
n = v[i].size();
for (j = 0; j < v[i].size(); j++) t += v[i][j];
printf("%.3lf\n", t/n);
}
}
}
|
Grading
- Prototype: 1 point
- Printing "Bad" (correctly): 1 point
- Using .3lf to print: 1 point
- Logic of the solution: 2 points
- Details: 3 points
Question 6: 8 points (2 per part)
- Part A: 37 = 2*16 + 5: 0x25
- Part B: Remember every two hex digits is a byte, and bytes represent
values from
0 to 255. Thus, the third digit from the right in hexadecimal is the 162,
or the 256 digit: 0x50f.
- Part C: 1 * 16 + 10 = 26.
- Part D: 256.
Question 7: 12 points
- Statement A: The constructor takes a parameter. Thus, you cannot
declare an Airplane without a parameter to the constructor: False.
- Statement B: This is true, because pilot is public --
anyone can mess with it: True.
- Statement C: Since N is protected, it may only be accessed
within Airplane's method implementations: False.
- Statement D: The order of declaration has nothing to do with
the order in which you call the methods: False.
- Statement E: The method implementations in airplane.cpp have
free reign over N: False.
- Statement F: Same as above: False.
- Statement G: Again, same description as E: True.
- Statement H: Sure, it can modify pilot, altitude or N.
It can also print stuff out or read/write files: False.
- Statement I: So true -- make it a return value -- declaring a small
value like time to be a reference parameter is an abomination. True.
- Statement J: Not at all -- since pilot is public, anything
can happen to it: False.
- Statement K: See Statement C: True.
- Statement L: Ditto: False.
- Statement M: See Statement A: True.
- Statement N: This is similar -- if you declare a to be a pointer
to an Airplane, then you need to call new to create an instance.
Since the constructor takes a parameter, you need to give it a parameter: True.
- Statement O: See Statement I: False.
- Statement P: This one is going to fail, because the constructor is
defined to take an integer, not a float or a double: False.
- Statement Q: That is exactly how you read command line arguments: True.
- Statement R: See Statement Q: False.
Grading
Each statement was .75 points with the exeception of C, E, F, G, K, and L which were .5 points.
Question 8: 8 points
This is from SRM 297, Division 2, 250-pointer. A solution
is in q8.cpp. Obviously, yours can differ
somewhat, but most solutions will look something like the one below:
#include "q8.h"
int PackingParts::pack(vector <int> partSizes, vector <int> boxSizes)
{
int p, b, max;
max = 0;
b = 0;
for (p = 0; p < partSizes.size(); p++) {
while (b < boxSizes.size() && boxSizes[b] < partSizes[p]) b++;
if (b == boxSizes.size()) return max;
b++;
max++;
}
return max;
}
|
I've put the class definition into q8.h, and
I've written a main() routine that takes the example number as a
command line arguments and prints the return value of pack().
That is in q8-Main.cpp.
Here's how they compile and run:
UNIX> g++ -o q8 q8.cpp q8-Main.cpp
UNIX> q8 0
3
UNIX> q8 1
2
UNIX> q8 2
3
UNIX> q8 3
4
UNIX> q8 4
3
UNIX> q8 5
6
UNIX>
Many of you gave a solution like the following:
int PackingParts::pack(vector <int> partSizes, vector <int> boxSizes)
{
int p, b, max;
max = 0;
for (p = 0; p < partSizes.size(); p++) {
for (b = 0; b < boxSizes.size(); b++) {
if (boxSizes[b] >= partSizes[p]) {
boxSizes[b] = 0;
max++;
b = boxSizes.size(); // Or "break"
}
}
}
return max;
}
|
I gave full credit to this, because the size of the vectors is small (< 50 each). However,
this solution is not the best, because the running time will be roughly
partSizes.size()*boxSizes.size().
My solution will be max(partSizes.size(),boxSizes.size()), which is much faster.
This is an example of code that will work with topcoder, but isn't "good" code. Hence why I bring
it up.
Grading
4 points for logic. 4 points for details.