Winter 2005 CS 31 Practice Midterm Answer Key ------------------------ PART I. ------------------------ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 d c b b a b c d a c c e d d e c c d ------------------------ PART II. ------------------------ --- Q19 --- Please refer to the online supplements to lecture. --- Q20 --- A variable is an object with an identifier. Please see the online terminology page for the list of attributes. --- Q21 --- puzzle(s) displays the ASCII values of the characters stored in string s. --- Q22 --- riddle(n, b) displays the base-b representation of the positive integer n, as long as b > 1 and n > 0. If b == 1 and n > 0, an infinite recursion results. If b <= 0 or n <= 0, the function does nothing. --- Q23 --- Please compare to the online GPA example. ------------------------ PART III. ------------------------ Question 24. void drawRectangle (int height, int width, char fill) { for (int i = 1; i <= height; i++) { for (int j = 1; j <= width; j++) cout << fill; cout << endl; } } Question 25. void drawRecRow (int height, int width, string fill) { for (int i = 1; i <= height; i++) { for (int j = 0; j <= fill.size(); j++) for (int k = 1; k <= width; k++) cout << fill[j]; cout << endl; } } Question 26. string leftShift (string str) { if (str.size() == 0) return str; else return str.substr(1, str.size()-1) + str.substr(0,1); } Question 27. void drawQuilt (int height, int width, string fill) { for (int i = 1; i <= fill.size(); i++) { drawRecRow(height, width, fill); fill = leftShift(fill); } }