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


Exam 3 Review - Summer 2001

This page gives you guidance on what to study for Exam 3. In general, the Self-Review Exercises at the ends of the chapters are good to look over and the answers are given in the book. Answers to other questions are here. Feel free to discuss the answers with your classmates or me (this is a good place to use the listserve).

Points on the exam will be distributed roughly as follows:

Structs and Classes

60%

Streams and I/O

included in questions on other topics

File Processing

20%

Dynamic Memory Allocation

10%

Linked Lists

10%

Chapter 6 Classes

You will be responsible for most of the material in Chapter 6.

Not covered on the exam:
Section 6.12 Using Destructors

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. Create a simple class called Pokemon with private data members:

Include public member functions:

8. 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.

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

10. 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.

11. 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.

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

13. 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.

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

Chapter 11 Streams and I/O

Important topics which you should know:

Not covered on the exam:

Chapter 14 File Processing

Only sections 14.1 through 14.6 will be covered on the exam.

Important topics which you should know:

Not covered on the exam:
Random files

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.

Dynamic Memory Allocation
section 7.6 - new and delete operators
sections 15.1 - 15.3 - dynamic memory allocation

Know how to dynamically allocate memory to store:

For example, 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;

Take a look at the following Example C++ Programs that dynamically allocate structs or class objects and store their addresses in an array of pointers:

Questions:

1. 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.

2. 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.

3. 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.

4. 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.

5. 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.

6. Given the Pokemon class declared in a previous problem, write client code to create a varaible 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.

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

8. 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.

9. 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.

10. 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.

11. 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.

Chapter 15 - Data Structures
sections 15.1 - 15.4 only (linked lists)

 

You should know what a linked list is and basically how it works. You will not have to write any code to manipulate a linked list. You do not need to know material from Chapter 12 on Templates.

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?


Return to C++ Home Page

Copyright: Ó 2001 by the Austin Community College.
Department of Computer Studies. All rights reserved.
Comments to:
Bob Comer
Last updated: July 21, 2001