CIS 2003 - Introduction to C++ and C Programming
Bob Comer, Professor, CIS/CSC
Exam 3 Review Exercises
To prepare for Exam 3, I recommend that you work throughthese exercises. I will NOT grade these exercises. Some answers are given in the book, other answers 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:
|
Chapter 12 - Arrays (2-D, of structs, of objects) |
30% |
|
Chapter 13 - Lists, searching, sorting |
15% |
|
C strings |
25% |
|
Chapter 15 - Pointers, dynamic memory allocation, arrays and pointers (supplement) |
20% |
|
Chapter 16 - Linked Lists |
10% |
Chapter 12 - Arrays (2D, of structs, of objects)
Other Questions:
1. Write a value-returning function that calculates and returns the sum of the elements in a two-dimensional array of int values. Assume that global constant NUM_COLS contains the number of columns in the array. The array and the number of rows in array should be parameters to the function.
2. Write a function that sets all values in a two-dimensional array of double values to 1.0. Assume that global constant NUM_COLS contains the number of columns in the array. The array and the number of rows in array should be parameters to the function.
3. Write a declaration for a struct called Pokemon (pocket monster) that has data members:
4. Write a 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 7.
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 declare an array of 150 Pokemon called pokedex.
9. 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 7.
10. 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.
11. Write a function that returns a count of the number of Pokemon in array pokedex that have strength greater than 6.
Chapter 13 - Lists. Searching, Sorting, C strings
Know how to create a C string (character array) to hold a given number of characters. Be sure you understand the difference between using the extractor operator to read strings and using the get() or getline() functions. I expect you to know how to use the basic string functions: strcpy(), strcmp(), and strlen().
Other questions:
1. Write a C++ function that searches an integer list that is sorted in increasing order and returns the index of the first element that is larger than a specified value. The search value, the list, and the size of the list should be parameters to the function.
2. Array list contains 21 elements. Write C++ code to use your function from problem 1 to print the index of the first element in array list that is over 50.
Chapter 15 - Pointers, dynamic memory allocation, arrays and pointers supplement
Know how to dynamically allocate memory to store:
For example, the following code dynamically allocates an integer, stores a value in it, and then prints it out.
int *year; year = new int; *year = 2000; cout << "The year is " << *year << endl;
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 7.
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 7.
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 7.
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 7.
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.
Chapter 17 - Linked lists
You are responsible for knowing the material on pages 943 - 956 of the text. You will need to know what a linked list is, how they are used, and when they should be used. You will need to know the advantages and disadvantages of using a linked list versus using an array. You will not have to write any code to manipulate a linked list.
Copyright: Ó 2000 by the Austin Community College.
Department of Computer Studies. All rights reserved.
Comments to: Bob Comer
Last updated: August 3, 2000