COSC 1337 - Programming Fundamentals II
Bob Comer, Professor of Computer Studies
Chapter 10 Pointers
Review Exercises
I recommend that you review the chapter by doing these exercises. I will NOT grade these exercises.
Consider reviewing the Checkpoint questions from this chapter.
Answers to Checkpoint questions are in Appendix P (on the CD that came with your textbook).
Review Questions and Exercises: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29.
Answers to Review Questions and Exercises are in Appendix Q (on the CD that came with your textbook).
Other Questions
1. I expect you to be able to manipulate the value of a variable of a simple type using a pointer. This is not particularly useful, it just demonstrates that you know how pointers work. For example, given integer variable num, write code to declare a pointer variable called numPtr and store the address of num in numPtr. Then write code to change the value of num to 5 and print it's value without using the variable name (use the pointer).
2. The following function (shown with sample call) uses reference parameters to exchange the values of the arguments in the call. Re-write the function to use pointers instead of reference parameters (re-write the sample call also). Note: when you want to change the values of the arguments in a function call, you really should use reference parameters, not pointers. This is just an exercise to show how pointers work.
Call:
int x = 10, y = 20; swap( x, y );
function definition:
void swap( int& num1, int& num2 ) { int temp; temp = num1; num1 = num2; num2 = temp; }
3. I expect you to be able to dynamically allocate memory for a value of a simple data type. For example, write code to dynamically allocate memory for a float value, store 6.5 in it, and then print it's value.
4. I expect you to be able to dynamically allocate an array of a simple data type. For example, write code to dynamically allocate an array of 5 integers, initialize the array to { 10, 20, 30, 40, 50 }, and then print the values in the array.
5. I expect you to know the relationship between pointers and arrays in C++.(see section 10.3). Here are some notes that I wrote on the topic. Click here for some notes on manipulating arrays using pointers.
Questions 6 - 10 refer to this struct declaration:
struct Item { int itemNo; char desc[41]; float price; };
6. Write the declaration for a variable called itemPtr that can hold the address of an Item struct. Then dynamically allocate an Item struct and set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.
7. Write a function that takes as parameters a pointer to an Item struct, an item number, a description, and a price. Your function should assign the item number, description, and price to the struct data members.
8. Write a declaration for an array of 150 pointers to structs of type Item called itemPtrs. Then write code to dynamically allocate an Item struct, store it's address in the first element of itemPtrs, then set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.
9. Write a function that prints the descriptions of all the elements in array itemPtrs where the price is 2.0 or over. You can assume that the array is full (each element of the array points to an Item object).
10. Write a function that returns a count of the number of Items in array itemPtr whose price is 2.0 or greater. You can assume that each element of the array points to an Item object.
11. Write a declaration for a structure to hold a date as 3 integer values: the month, day, and year. Then write code to dynamically allocate a date, store some information in it, and print it out.
A2. Reviw the following Example C++ Program that dynamically allocates structs and stores their addresses in an array of pointers:
The following questions (12 - 17) refer to this class declaration:
class Item { private: int itemNo; // inventory item number char desc[61]; // item description float price; // item price public: Item(); Item(int, char [], float); void set(int, char [], float); int getItemNo(); const char *getDesc(); float getPrice(); }; Item::Item() { itemNo = 0; strcpy( desc, ""); price = 0; } Item::Item(int item, char itemDesc[], float itemPrice) { set(item, itemDesc, itemPrice); } void Item::set(int item, char itemDesc[], float itemPrice) { itemNo = item; strcpy( desc, itemDesc ); price = itemPrice; } int Item::getItemNo() { return itemNo; } const char * Item::getDesc() { return desc; } float Item::getPrice() { return price; }
12. Class objects can be dynamically allocated similar to the way structs are dynamically allocated. Given the Item class declared above, write client code to create a variable to hold the address of an Item object. Then dynamically allocate an Item object and set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.
13. Write client code to declare an array called itemPtrs of 150 pointers to Item objects.
14. Write client code to dynamically allocate an Item object, store it's address in the first position of array itemPtrs, and set it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.
15. Write a function that prints the descriptions of all the elements in array itemPtrs where the price is 2.0 or over. You can assume that the array is full (each element of the array points to an Item object).
16. Write a function that returns a count of the number of Items in array itemPtr whose price is 2.0 or greater. You can assume that each element of the array points to an Item object.
17. Given that the Item class contains a second constructor that allows the client programmer to declare an Item object and specify it's initial values in a single statement, write a statement that dynamically allocates an Item object and initializes it's item number to 10, it's description to "Granny Smith Apples", and it's price to 1.89.
A1. Review the following Example C++ Program that dynamically allocates class objects and stores their addresses in an array of pointers:
Answers to Other Questions are here.
Return to Programming Fundamentals II Home Page
Copyright: © 2013 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to: Bob Comer
Last updated: July 13, 2013