Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Standard view
Full view
of .
0 of .
Results for:
P. 1
h31 midterm solution

# h31 midterm solution

Ratings:

4.0

(1)
|Views: 62|Likes:

### Availability:

See more
See less

02/01/2013

pdf

text

original

CS106B
Handout #31
Yves Lu
July 30, 2008
Midterm Solutions

We have graded exams to return at the end of today's lecture. Be sure to thank our hard-working section leaders who gave up their Tuesday night to decipher your handwriting! Overall the exam was probably a little more time pressured than we would have liked, but most people were able to get through all of the problems. You guys showed a solid understanding of the basics of recursion and the use of the various classes we\u2019ve covered so far.

For the exam, the mean was 67, the median 74 and the standard deviation 21. Here\u2019s a
histogram of the scores:

The midterm is a small but relevant part of your grade (15%). If it didn't go so well for you, we will offer you some encouragement to do better on the final exam. If you do, we will minimize the midterm's impact and shift some of its weight to the final.

Regrades: if there was an error in scoring your exam, please submit it to Te no later than a week from today for correction. The entire exam will be re-evaluated. Please return the grading sheet with your exam.

Problem 1: C++ coding
int DecodeWord(string s) {
int sum = 0;
string lowered = ConvertToLowerCase(s);

for(int i = 0; i < s.length(); i++) {
char ch = lowered[i];
int val = ch -'a' + 1;

sum += val;
}return(sum);
}string *DecodeFiles(string filename[],int numFiles, string key) {
string *decodedMsgs =new string[numFiles];
for(int i = 0; i < numFiles; i++) {
ifstream infile;
infile.open(filename[i].c_str());
if(infile.fail()) Error("Could not open file.");
string word, message;
intindex;
infile >> word;
while(!infile.fail()) {

index = DecodeWord(word);
message += key[index];
infile >> word;

}decodedMsgs[i] = message;
}return(decodedMsgs);
}Grading (mean for this question: 19/25)
Overall, people didn\u2019t seem to have too much trouble with this problem. Common mistakes

were forgetting to check inFile.fail() right after reading from the file and also after pulling in a
word from the file. In addition, some people had problems declaring an array of strings on the
heap to return, or just returned one declared on the stack. One additional thing to note is that you
can easily convert from a character to its integer value as the solution shows above.

Problem 2: Recursion

int happiness = 0;
for(int i = 0; i < arrangement.size(); i++) {

if(inBounds(arrangement, i - 1)) {
if(arrangement[i]->enemies.contains(arrangement[i-1]))
return(-1);
if(arrangement[i]->friends.contains(arrangement[i-1]))
happiness++;
}if(inBounds(arrangement, i + 1)) {
if(arrangement[i]->enemies.contains(arrangement[i+1]))
return(-1);
if(arrangement[i]->friends.contains(arrangement[i+1]))
happiness++;
}
}return(happiness);
if(happiness == -1 || leadersLeft.size() == 0) {
int newHappiness = -1;
for(int i = 0; i < leadersLeft.size(); i++) {

newLeft.removeAt(i);

if(newHappiness > happiness) {
best = newArrangement;
happiness = newHappiness;
newArrangement =