ASSESSMENT 1 SOLUTION --------------------- Problem 1 (Number Representation) --------- Topic: Number representation Answer: 6B (hex) Explanation: We seek a number of the form: a(n) a(n-1) ... a(1) a(0) such that: a(n) 16^n + ... + a(1) 16^1 + a(0) = 107 (decimal) Since: floor(107/16) = 6 rem(107, 16) = 11 and 11 (decimal) = B (hexadecimal) we arrive at our answer. floor(x) is the largest integer less than or equal to x, and rem(x,y) is the remainder of x/y. Problem 2 (Arithmetic Operators, While-Loop) --------- Topic(s): arithmetic operators, while-loop Answer: 765 Explanation: 'c' accumulates the remainder of successively dividing a by 10. Thus, it creates in 'c' the right-to-left digits. Problem 3 (Array, For-Loop) --------- Topic(s): array, for-loop 3, the minimum integer from the integer array n[10]. Explanation: o j is initially 10, the value of the last item o The loop scans the array backwards o The only time that j is modified is if it is larger than an item. Thus, it will return the minimum integer. Problem 4 (Integer Pointers) --------- Topic(s): integer pointers a) 10 Explanation: Since px points to x, (*px)++ will increment x. But since the autoincrement is in postfix form, x is used first and then its value is incremented (after x has been assigned to z). b) 40 Explanation: py points to y. (*px)+y just adds y to itself. c) Invalid since x and y are not pointers. Problem 5 (String Pointers) --------- Topic(s): string pointers Answer: bcd accd Explanation: p initially points at the character 'a', the first element of s[]. After incrementing, it points at 'b'. '(*p)++ increments the character 'b'. Because of the ASCII character representation, the 'b' becomes 'c'. The first printf prints the substring of s starting from the 2nd character. The second printf prints the entire (modified) string s. Problem 6 (Array of String Pointers) --------- Topic(s): array of string pointers A[] is an array of C-string pointers: A[0] ----> "I" A[1] ----> "am" A[2] ----> "in" A[3] ----> "CSE 422S" We want to overwrite part or all of the "CSE 422S" string stored at location A[3]. a) Solution 1: char *p = "422S"; char *pp = A[3]; int n = strlen(p); for (int i=0; i c.txt Problem 8 (Interfaces, Abstract Data Types) --------- Topic(s): interfaces <<< C++ Version >>> SymTbl *s = new SymTbl(10); printf("Before: a = %d, b = %d\n", s->value("a"), s->value("b")); s->set("a", 2); s->set("b", 3); printf("After: a = %d, b = %d\n", s->value("a"), s->value("b")); Comment: Here is an alternative form that uses a different form of allocation. SymTbl s(10); printf("Before: a = %d, b = %d\n", s.value("a"), s.value("b")); s.set("a", 2); s.set("b", 3); printf("After: a = %d, b = %d\n", s.value("a"), s.value("b")); Here, the allocation is done by the compiler. <<< C Version >>> SymTbl_t s = SymTbl(10); printf("Before: a = %d, b = %d\n", value(s, "a"), value(s, "b")); set(s, "a", 2); set(s, "b", 3); printf("After: a = %d, b = %d\n", value(s, "a"), value(s, "b")); Comment: Note the similarity between the C and C++ code. In the C version, the pointer 's' is passed into the set function and is used to locate the internal storage (state) associated with the symbol table. In fact, C++ implements the method calls in exactly the same manner for these simple methods.