Question 1 was from the first midterm in Winter 2005 Question 2 was from an exam in Fall 2004 (first time those students saw Game of Life) Question 3 was based on a question from the textbook (page 101) Question 4 was part from the textbook (page 69), part from a Winter 2005 quiz Question 5 was new 1. 20 pts, 5 pts per fill-in // if nterms = 1 this should compute 1 // if nterms = 2 this should compute 1 + 1/4 // if nterms = 3 this should compute 1 + 1/4 + 1/9 // if nterms = 4 this should compute 1 + 1/4 + 1/9 + 1/16 // each new term adds an inverse square double sum = 0; // need to initialize sum int i= 1; // 1/(i*i) is bad if i=0 while (i <= nterms) // <= to get correct number of terms { term = 1.0 / (i*i); // 1/(i*i) is integer division sum = sum + term; i = i + 1; } 2. 20 pts, 4 pts per fill-in // sum contains the sum of "live" // cells in the 3x3 grid around ic,jc return sum - table[ic][jc]; // must exclude middle cell from total // There are various ways to implement the rules. One possibility: if (count == 3) // when there are 3 neighbors { updated[i][j] = 1; // next generation is always alive } else if (count == 2 and table[i][j] == 1 ) // 2 neighbors & alive { updated[i][j] = 1; // means it stays alive next generation } 3. 20 pts, roughly 2 pts per error, multiple solutions possible #include using namespace std; int main() { double x; cout << "Input x "; cin >> x; // was cin << x; (syntax) double p=1; // was double p=0; (logic) double sum=1; // was double sum; (logic) int i=2; // was not present (syntax/logic) while (i<=N) // removed ; (syntax) { p = p * x * x; // was just p=p*x; (logic) sum = sum + p; i = i + 2; // was not present (logic) } cout << "Sum = " << sum << endl; // added ; (syntax) return 0; } 4. 20 pts divided 2/2/3/3/2/2/3/3 22 base 10 = 10110 base 2. // 16 + 4 + 2 44 base 10 = 101100 base 2. // Note 44 = 2*22, so add a zero! AB base 16 = 10101011 base 2. // AB is a 2-digit number, not A * B // A = 1010, B = 1011 FFE3 + 4FA9 = 14F8C. // partial credit given for 14F9C // but 3 + 9 = C without carry // E + A = 8 carry a 1 (base 16) // F + F + 1 = F carry a 1 // F + 4 + 1 = 4 carry a 1 double p = 1 + 2 + 3/4 + 5; // 3/4 is integer division = 0 // proper answer is 8.0 (p is a double) // 8 (written as an integer) accepted int k = 10 % 3; // % is remainder after division // answer = 1 int j = 13/5 + 13%5 // 13/5 = 2 (drop fraction) // 13%5 = 3 (remainder) // answer = 5 int n = 1-(static_cast(3*5/4.0)/2); // start with inner most parenthesis just like in math // 3 * 5 is 15 // 15 / 4.0 is 3.75 (not integer division, keeps fraction) // integer part of 3.75 is 3 (static_cast drops fraction) // integer division 3/2 is 1 // 1 - 1 gives answer = 0 5. 20 pts, various ways to write the if-condition are possible #include using namespace std; int main() { int age; cout << " Enter your age "; cin >> age; cout << "Hello, you are old enough to purchase "; if (age >= 21) { cout << "booze and cigarettes." << endl; } else if (age < 18) { cout << "neither booze nor cigarettes." << endl; } else // case of age >= 18 and age < 21 { cout << "cigarettes, but not booze." << endl; } }