{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Project 6 Spec

Project 6 Spec - Spring 2009 CS 31 P Project 6Get the Point...

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

View Full Document Right Arrow Icon
Spring 2009 CS 31 Project 6 P Get the Point? Time due: 9:00 PM Wednesday, May 27 This project is designed to help you master pointers. To that end, you'll get the most out of it by working through the problems by hand. Only after that should you resort to running the programs to check your understanding. Remember, on the final exam you'll have to be able to do problems like this by hand. This "project" is more like a homework. There are five problems. In problems that ask you to change code, make the few changes necessary to fix the code without changing its overall approach. For example, don't fix the program in problem 1a by changing it to int main() { cout << " 30 20 10" << endl; } The subparts to this problem involve errors in the use of pointers. This program is supposed to write 30 20 10 , but it doesn't. Find all of the bugs and show a fixed version of the program: t int main() { int arr[3] = { 5, 10, 15 }; int* ptr = arr; *ptr = 10; // set arr[0] to 10 *ptr + 1 = 20; // set arr[1] to 20 ptr += 2; ptr[0] = 30; // set arr[2] to 30 while (ptr >= arr) { ptr--;
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
cout << ' ' << *ptr; // print values } cout << endl; } The findDisorder function is supposed to find the first item in an array that is less than the element preceding it, and set the p parameter to point to that item, so the caller can know the location of that item. Why won't this function do that, and how can it be fixed? Your fix must be to the function only; you must not change the the main routine below in any way, yet as a result of your fixing the function, the main routine below must work correctly. b void findDisorder(int arr[], int n, int* p) { for (int k = 1; k < n; k++) { if (arr[k] < arr[k-1]) { p = arr + k; return; } } p = NULL; } int main() { int nums[6] = { 10, 20, 20, 40, 30, 50 }; int* ptr; findDisorder(nums, 6, ptr); if (ptr == NULL) cout << "The array is ordered" << endl; else {
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}