Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
h31 midterm solution

h31 midterm solution

Ratings:

4.0

(1)
|Views: 62|Likes:
Published by minhhai2209

More info:

Published by: minhhai2209 on Feb 18, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
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 CalculateHappiness(Vector<leaderT *> arrangement) {
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);
}Vector<leaderT *> RecArrangeAndSeatLeaders(Vector<leaderT *> leadersSoFar,
Vector<leaderT *> leadersLeft,int &happiness) {
happiness = CalculateHappiness(leadersSoFar);
if(happiness == -1 || leadersLeft.size() == 0) {
happiness = CalculateHappiness(leadersSoFar);
return(leadersSoFar);
}Vector<leaderT *> best;
int newHappiness = -1;
for(int i = 0; i < leadersLeft.size(); i++) {

Vector<leaderT *> newSoFar = leadersSoFar;
leaderT *curLeader = leadersLeft[i];
Vector<leaderT *> newLeft = leadersLeft;
newLeft.removeAt(i);
Vector<leaderT *> newArrangement =

RecArrangeAndSeatLeaders(newSoFar, newLeft, newHappiness);
if(newHappiness > happiness) {
best = newArrangement;
happiness = newHappiness;
}newSoFar.add(curLeader);
newArrangement =
RecArrangeAndSeatLeaders(newSoFar, newLeft, newHappiness);
if(newHappiness > happiness) {
best = newArrangement;
happiness = newHappiness;
}
}return(best);
}

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->