{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Project 6 Solution

Project 6 Solution - Spring 2009 CS 31 Project 6 Solutions...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Spring 2009 CS 31 Project 6 Solutions First, because of the operators' order of precedence, the expression *ptr + 1 = 20 means (*ptr) + 1 = 20 . The expression (*ptr) + 1 evaluates to the int 11, not an int variable that can be assigned to. When corrected to *(ptr+1) = 20 , the expression means *(&arr[1]) = 20 , which means arr[1] = 20 . . Second, the while loop doesn't access arr[2] and tries to access arr[-1] . One possible fix is . ptr++; while (ptr > arr) { ptr--; cout << ' ' << *ptr; // print values } findDisorder puts the correct value in p , but p is a copy of the caller's variable ptr , so findDisorder has no effect on ptr . The parameter p must be passed by reference, not by value: void findDisorder(int arr[], int n, int* & p) The declaration double* p; declares p to be a pointer to double, but leaves it uninitialized — it does not point to any particular double. That uninitialized pointer is copied into the parameter resultPtr . In the expression *resultPtr = sqrt(…) , the attempt to dereference the uninitialized resultPtr pointer leads to undefined behavior. A fix would be to make sure
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}