Comp 1572, Winter 2002 Pencil-and-Paper Assignment 4 Assigned Monday, February 4, 2002 Due Monday, February 11, 2002 (At the beginning of class) ============================================================================ Solutions ============================================================================ 1. The following also includes corrections to the original code - main should be declared to return int, increment should be defined to return void, and the parameter list to increment should be comma-separated rather than with a semicolon. void increment(int *, int); int main() { int num(5); int inc_amount(2); increment(&num, inc_amount); } // increments value by increment_amt void increment(int *value, int increment_amt) { *value += increment_amt; } 2. void linear_search(int **, int, int); int main() { int arr[6] = {1, 26, 109, 42, 47, 1010}; int *iptr; iptr = arr; linear_search(&iptr, 6, 47); // search array for 47, memory location // of this key should be stored in iptr if (iptr) cout << *iptr; // I want it to display "47", but instead it displays "1" else cout << "Number not found\n"; } // ip points to beginning of array of length len. Search the array for key, // and "return" memory location of that value in ip void linear_search(int **ip, int len, int key) { for(int i=0; i< len; i++) if ((*ip)[i] == key) { *ip = &(*ip)[i]; return; } *ip = 0; // key not found! } 3. char p[] = "Yikes!"; char c='F'; const char *pc=p; char *const cp=p; const char * const ccp=p; char const *ppc=p; // warning, this one is a trick question! pc[5] = 'A'; // not okay, pc points to constant data pc = p; // Okay, pc is not a constant pointer *(cp) = 'Z'; // Okay, cp does not point to constant data cp = &c; // not okay, cp is a constant pointer ccp[5] = p[5]; // not okay, ccp points to constant data ccp = &p[3]; // not okay, ccp is a constant pointer *(ppc+1) = 0; // not okay, ppc points to constant data ppc = pc; // okay, ppc is is a non-constant pointer 4. First I wrote some code to test this function: void do_something(char ch) { cout << ch; } void traverse_with_index(char v[]) { for (int i=0; v[i] != 0; i++) do_something(v[i]); } int main() { char text[] = "This is the text"; traverse_with_index(text); return 0; } Then I rewrote the traverse_with_index function: void traverse_with_index(char *v) { for (char *p = v; *p != 0; p++) do_something(*p); } 5. string1 and string2 are concatenated, the result stored in string 1. string2 is not altered. The final concatenated result in string1 is displayed. 6. The function mystery2 counts the number of characters in the input string and returns that length. The length is displayed 7. a) The pointer variable called number does not point to a valid address. Before displaying the data, assign a valid address to the variable number. b) A pointer of type float cannot be directly assigned to a pointer of type long. c) Variable y is not a pointer, and tehrefore cannot be assigned to x. Change the assignment statement to x = &y; d) s is a constant pointer and thus cannot be modified. Traverse the array instead with an index, for example: for (int t = 0; s[t] != '\0'; t++) cout << s[t] << ' '; e) A void * pointer cannot be dereferenced f) xPtr is not a pointer and therefore cannot be assigned an address. Place a * before xPtr in the declaration to correct this. The cout statement then displays the address to which xPtr points - this is not necessarily an error. g) s does not point to anything. s should be set to a legitimate memory address before accessing it. 8. void quicksort(vector &nl, int low, int high) { int k; if (low > high) return; k = partition(nl, low, high); quicksort(nl, low, k-1); quicksort(nl, k+1, high); } // move the first element of array to its rightful position - return the // index of that position. Note: many element swaps are made - the only // guarantee after complete is that all elements to the left of the // partition element are smaller and all to the right are larger int partition(vector &nl, int low, int high) { int i(low), j(high); int partition_element = low; while (i < j) { while (nl.at(j) >= nl.at(partition_element) && i < j) j--; swap(nl, partition_element, j); partition_element = j; while (nl.at(i) <= nl.at(partition_element) && i < j) i++; swap(nl, partition_element, i); partition_element = i; } return i; } inline void swap(vector &v, int i, int j) { double temp; temp = v.at(i); v.at(i) = v.at(j); v.at(j) = temp; }