Project 6 Specs & Solution

Project 6 Specs & Solution - Project 6 What's the...

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

View Full Document Right Arrow Icon
Project 6 What's the Point? Time due: 9:00 PM Tuesday, November 22 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 (and stepping through them with the debugger) 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 << "10\n20\n30" << endl; } 1. The subparts to this problem involve errors in the use of pointers. a. This program is supposed to write 10 20 30 , one per line. Find all of the bugs and show a fixed version of the program: b. int main() c. { d. int arr[3] = { 5, 10, 15 }; e. int* ptr = arr; f. g. *ptr = 10; // set arr[0] to 10 h. *ptr + 1 = 20; // set arr[1] to 20 i. ptr += 2; j. ptr[0] = 30; // set arr[2] to 30 k. l. while (ptr >= arr) m. { n. ptr--; o. cout << *ptr << endl; // print values p. }
Background image of page 1

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

View Full DocumentRight Arrow Icon
q. } r. The findMax function is supposed to find the maximum item in an array and set the pToMax parameter to point to that item so that the caller knows its location. Explain why this function won't do that, and show how to fix it. Your fix must be to the function only; you must not change the main routine below in any way, yet as a result of your fixing the function, the main routine below must work correctly. s. void findMax(int arr[], int n, int* pToMax) t. { u. if (n <= 0) v. return; // no items, no maximum! w. x. pToMax = arr; y. z. for (int i = 1; i < n; i++) aa. { ab. if (arr[i] > *pToMax) ac. pToMax = arr + i; ad. } ae. } af. ag. int main() ah. { ai. int nums[4] = { 5, 3, 15, 6 }; aj. int* ptr; ak. al. findMax(nums, 4, ptr); am. cout << "The maximum is at address " << ptr << endl; an. cout << "It's at index " << ptr - nums << endl; ao. cout << "Its value is " << *ptr << endl; ap. } aq. The computeCube function is correct, but the main function has a problem. Explain why it may not work and show how to fix it. Your fix must be to the main function only; you must not change computeCube in any way. ar. void computeCube(int n, int* ncubed) as. { at. *ncubed = n * n * n; au. } av.
Background image of page 2
aw. int main() ax. { ay. int* ptr; az. computeCube(5, ptr); ba. cout << "Five cubed is " << *ptr << endl; bb. } bc. The strequal function is supposed to return true if and only if its two C string arguments have exactly same text. What are the problems with the implementation of the function, and how can they be fixed? bd.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/06/2012 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Page1 / 11

Project 6 Specs &amp;amp; Solution - Project 6 What's the...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online