COSC 1320 - C++ Programming
Bob Comer, Professor, CIT/CSC


Exam 3 Review - Spring 2002

To prepare for Exam 3, I recommend that you look over the following exercises taken from Starting Out with C++ Brief second edition by Tony Gaddis. If you have C++ How to Program second edition by Deitel and Deitel, click here for review exercises. I will NOT grade these exercises. Feel free to discuss the answers with your classmates or me (this is a good place to use the class listserve).

Answers to all Checkpoint questions are in Appendix K. Answers to odd-numbered Review Questions are in Appendix L. Answers to other questions are here. Remember that most exam questions will ask you to write C++ code.

Points on the exam will be distributed roughly as follows:

Structs and Classes

60%

File Processing

20%

Dynamic Memory Allocation

10%

Linked Lists

10%

Dynamic Memory Allocation

Review material on dynamic memory allocation from chapters prior to chapter 10. Know how to dynamically allocate memory to store a simple data type (float, int, double, etc.) or an array of a simple data type.

Chapter 10 Structured Data

Not covered on the exam:
Section 10.11 Unions

Questions:

1. Write the declaration for a struct called Pokemon (pocket monster) that has data members:

2. Write a declaration for a Pokemon struct variable called myMonster and then write code to set it's name to "Charmander", it's type to "Flame", and it's strength to 5.

3. Write a function that takes as parameters a Pokemon struct, a name, a type, and a strength. Your function should assign the name, type, and strength to the struct data members.

4. Write the declaration for an array of 150 Pokemon called pokedex. Then write code to set the name of the first Pokemon in pokedex to "Charmander", it's type to "Flame", and it's strength to 5.

5. Write a function that prints a list of all the Pokemon in array pokedex of type "Flame". You can assume that the array is full.

6. Write a function that returns a count of the number of Pokemon in array pokedex that have strength greater than 6.

7. Write the declaration for a variable called monsterPtr that can hold the address of a Pokemon struct. Then dynamically allocate a Pokemon struct and set it's name to "Charmander", it's type to "Flame", and it's strength to 5.

8. Write a function that takes as parameters a pointer to a Pokemon struct, a name, a type, and a strength. Your function should assign the name, type, and strength to the struct data members.

9. Write a declaration for an array called pokedex of 150 pointers to Pokemon . Then write code to dynamically allocate a Pokemon, store it's address in the first element of pokedex, then set it's name to "Charmander", it's type to "Flame", and it's strength to 5.

10. Write a function that prints a list of all the Pokemon in array pokedex of type "Flame". You can assume that each element of the array points to a Pokemon object.

11. Write a function that returns a count of the number of Pokemon in array pokedex that have strength greater than 6. You can assume that each element of the array points to a Pokemon object.

A1. Following is an example of dynamically allocating a structure. The following code declares a Date structure, dynamically allocates a Date, stores information in the Date and then prints it out.

struct Date
{
   int month;
   int day;
   int year;
};

Date *hired;
hired = new Date;
hired->month = 12;
hired->day = 1;
hired->year = 1999;
cout << "Hire date: "
     << hired->month << '\\'
     << hired->day << '\\'
     << hired->year % 100 << endl;

A2. Reviw the following Example C++ Program that dynamically allocates structs and stores their addresses in an array of pointers:

itemptr.cpp

Chapter 11 Introduction to Classes

1. Create a simple class called Pokemon with private data members:

Include public member functions:

2. Write client code to create a Pokemon object and then set it's name to "Charmander", it's type to "Flame", and it's strength to 5.

3. Write client code to declare an array of 150 Pokemon called pokedex.

4. Write client code to set the name of the first Pokemon in pokedex to "Charmander", it's type to "Flame", and it's strength to 5.

5. Write a function that prints a list of all the Pokemon in array pokedex of type "Flame". You can assume that the array is full.

6. Write a function that returns a count of the number of Pokemon in array pokedex that have strength greater than 6.

7. Creating a Pokemon object and setting it's data members to values specified by the client programmer takes 2 statements with the Pokemon class code written so far. Add a second constructor to the class so that the client programmer can declare a Pokemon object and specify it's initial values in a single statement.

8. Write a sample call to the constructor written in the previous problem.

9. Class objects can be dynamically allocated similar to the way structs are dynamically allocated. Given the Pokemon class declared in a previous problem, write client code to create a variable to hold the address of a Pokemon object. Then dynamically allocate a Pokemon object and set it's name to "Charmander", it's type to "Flame", and it's strength to 5.

10. Write client code to declare an array called pokedex of 150 pointers to Pokemon objects.

11. Write client code to allocate a Pokemon object, store it's address in the first position of array pokedex, and set it's name to "Charmander", it's type to "Flame", and it's strength to 5.

12. Write a function that prints a list of all the Pokemon in array pokedex of type "Flame". You can assume that each element of the array points to a Pokemon object.

13. Write a function that returns a count of the number of Pokemon in array pokedex that have strength greater than 6. You can assume that each element of the array points to a Pokemon object.

14. Given that the Pokemon class contains a second constructor that allows the client programmer to declare a Pokemon object and specify it's initial values in a single statement, write a statement that allocates a Pokemon object and initializes it's name to "Raichu", it's type to "Mouse", and it's strength to 7.

A1. Review the following Example C++ Program that dynamically allocates class objects and stores their addresses in an array of pointers:

itemptr2.cpp

Chapter 12 More on Classes

Be familiar with the material in Section 12.3.

Chapter 14 File Operations

Not covered on the exam:
Section 14.15 Binary Files
Section 14.16 Creating Records using Structures
Section 14.17 Random Access Files
Section 14.18 Opening a File for Both Input and Output

Questions:

1. Given the following declarations from a previous exercise:

struct Pokemon
{
   char name[21];
   char type[21];
   short strength;
};

Pokemon pokedex[150];

Write code to read data from file c:\pokemon.txt and load it into the array until the end of the file is reached, or the array is full, whichever comes first. Include code to open the file.

2. Write code to take the data in array pokedex and write it into file c:\pokemon.txt. Include code to open the file.

Appendix C Linked Lists

Not covered:
Section C.3 Does not exist
Section C.4 Variations of the Linked List 

You should know what a linked list is and basically how it works. Focus on the material on pages 733 - 738. You will not have to write any code to manipulate a linked list.

Questions:

  1. Under what circumstances might it be better to use a linked list instead of an array of structs or objects? Under what circumstances might it be better to use an array of structs or objects instead of a linked list?
  2. Issues: max size, shrink/grow, editing (insert/delete), direct access vs traversal.


Return to C++ Home Page

Copyright: Ó 2002 by the Austin Community College.
Department of Computer Studies. All rights reserved.
Comments to:
Bob Comer
Last updated: May 2, 2002