• Home
  • Textbooks
  • C++ Plus Data Structures
  • Data Design and Implementation

C++ Plus Data Structures

Nell Dale, Chip Weems, Tim Richards

Chapter 2

Data Design and Implementation - all with Video Answers

Educators


Chapter Questions

Problem 1

Explain what we mean by "data abstraction."

Check back soon!

Problem 2

What is data encapsulation? Explain the programming goal "to protect our data abstraction through encapsulation."

Check back soon!

Problem 3

Name three perspectives from which we can view data. Using the logical data structure "a list of student academic records," give examples of what each perspective might tell us about the data.

Check back soon!

Problem 4

Consider the ADT GroceryStore.
1. At the application level, describe GroceryStore.
2. At the logical level, what grocery store operations might be defined for the customer?
3. Specify (at the logical level) the operation CheckOut.
4. Write an algorithm (at the implementation level) for the operation CheckOut.
5. Explain how parts (c) and (d) represent information hiding.

Check back soon!

Problem 5

What composite types are predefined in the C++ language?

Check back soon!

Problem 6

Describe the component selectors for structs and classes at the logical level.

Check back soon!

Problem 7

Describe the accessing functions for structs and classes at the implementation level.

Check back soon!

Problem 8

Describe the component selectors for one-dimensional arrays at the logical level.

Check back soon!

Problem 9

Describe the accessing functions for one-dimensional arrays at the implementation level.

Check back soon!

Problem 10

1. Declare a one-dimensional array, name, that contains 20 characters.
2. If each character occupies one "cell" in memory, and the base address of name is 1000 , what is the address of the cell referenced in the following statement?
name $[9]=$ 'A';
Use the following declarations for Exercises 11 and 12:
```
enum MonthType {JAN, FEB, MAR, APR, MAY
OCT, NOV, DEC};
struct WeatherType
{
int avgHiTemp;
int avgLoTemp;
float actualRain;
float recordRain;
};
```
Assume that an int requires one cell in memory, that a float number requires two cells, and that the struct members are found in contiguous memory locations with no gaps.

Check back soon!

Problem 11

1. Declare a one-dimensional array type, WeatherListType, of WeatherType components, to be indexed by values of type MonthType. Declare a variable, yearlyweather, of WeatherListType.
2. Assign the value 1.05 to the actual rainfall member of the July record in yearlyWeather.
3. If the base address of yearlyweather is 200 , what is the address of the member that you assigned in part (b)?

Check back soon!

Problem 12

1. Declare a two-dimensional array, decadeWeather, of Weather Type components, to be indexed by values of type MonthType in the first dimension.
2. Draw a picture of decadeWeather.
3. Assign the value 26 to the avgLoTemp member of the March 2006 entry.

Check back soon!

Problem 13

1. Define a three-dimensional array at the logical level.
2. Suggest some applications for three-dimensional arrays.
Use the following declarations for Exercises 14-16.
```
typedef char String[10];
struct StudentRecord
{
String firstName;
String lastName;
int id;
```
```
float gpa;
int currentHours;
int totalHours;
};
StudentRecord student;
StudentRecord students [100];
```
Assume that an int requires one cell in memory, that a float number requires two cells, and that the struct members are found in contiguous memory locations with no gaps.

Check back soon!

Problem 14

Construct a member-length-offset table for StudentRecord.

Check back soon!

Problem 15

If the baseaddress of student is 100 , what address does the compiler generate as the target of the following assignment statement?
student. gpa $=3.87$

Check back soon!

Problem 16

How much space does the compiler set aside for students?

Check back soon!

Problem 17

Indicate which predefined C++ types would most appropriately model each of the following (more than one may be appropriate for each):
1. a chessboard
2. information about a single product in an inventorycontrol program
3. a list of famous quotations
4. the casualty figures (number of deaths per year) for highway accidents in Texas from 1995 to 2005
5. the casualty figures for highway accidents in each of the states from 1995 to 2005
6. the casualty figures for highway accidents in each of the states from 1995 to 2005, subdivided by month
7. an electronic address book (name, address, and phone information for all your friends)
8. a collection of hourly temperatures for a 24-hour period

Check back soon!

Problem 18

What $\mathrm{C}++$ construct is used to represent ADTs?

Check back soon!

Problem 19

Explain the difference between a C++ struct and class.

Check back soon!

Problem 20

How is the client prevented from directly accessing the details of an instance of a class?

Check back soon!
01:58

Problem 21

1. The details of a private member can be seen by the user of a class. (True or False?)
2. The details of a private member may be accessed by a client program. (True or False?)

Crystal Wang
Crystal Wang
Numerade Educator

Problem 22

Why is it good practice to put a class declaration in one file and the implementation in another?

Check back soon!
02:44

Problem 23

Name three ways that classes can relate to each other.

Prathan Jarupoonphol
Prathan Jarupoonphol
Numerade Educator

Problem 24

Distinguish between composition and inheritance.

Check back soon!

Problem 25

Distinguish between a base class and a derived class.

Check back soon!

Problem 26

Does a derived class have access to the private data members of the base class?

Check back soon!

Problem 27

Does a derived class have access to the public member functions of the base class?

Check back soon!

Problem 28

1. Write the specification for an ADT SquareMatrix. (A square matrix can be represented by a two-dimensional array with N rows and N columns.) You may assume a maximum size of 50 rows and columns. Include the following operations:
MakeEmpty( n$)$, which sets the first n rc StoreValue(i, j, value), which stores a Add, which adds two matrices together Subtract, which subtracts one matrix fr Copy, which copies one matrix into anot
2. Convert your specification to a $\mathrm{C}++$ class declaration.
3. Implement the member functions.
4. Write a test plan for your class.

Check back soon!

Problem 29

DateType keeps only the integer representation of the month, day, and year. When a month is wanted in string form, the string is calculated. An alternate approach would be to add a string field to the date and calculate and store the string representation in the Initialize function. Which methods would have to be changed? Would this change make the use of an if statement to find the appropriate string more or less attractive? Write the code for this if statement.

Check back soon!

Problem 30

What changes would be necessary if the number of days in the month were carried as a data field in class DateType rather than being looked up when necessary? Would this change make the use of a switch statement to find the number of days in the month more attractive? Write the code for this switch statement.

Check back soon!
05:39

Problem 31

Compare and contrast the implementation of class DateType used in the Case Study and the solution proposed in Exercises 29 and 30 . These two approaches represent the classic trade-off between space and algorithm complexity. Please comment.
Use the following possible answers for Exercises 32-36.
1. $O(1)$
2. $\mathrm{O}(\log \mathrm{N})$
3. $\mathrm{O}(\mathrm{N})$
4. $\mathrm{O}(\mathrm{N} \log \mathrm{N})$
5. $\mathrm{O}\left(\mathrm{N}^* \mathrm{~N}\right)$
6. $\mathrm{O}\left(\mathrm{N}^{\prime \prime} \mathrm{N}^{-} \mathrm{N}\right)$

Eric Mockensturm
Eric Mockensturm
Numerade Educator

Problem 32

The order of sorting an array of N items using one of the better sorting algorithms such as Quicksort.

Check back soon!

Problem 33

The order of sorting an array of N items using one of the slower sorting algorithms such as SelectionSort.

Check back soon!
00:20

Problem 34

The order of an algorithm that decrements every element in a three-dimensional table of N rows.

Amy Jiang
Amy Jiang
Numerade Educator
00:42

Problem 35

The order of an algorithm that increments every element in a twodimensional table of N rows.

Melissa Stefan
Melissa Stefan
Numerade Educator

Problem 36

The order of comparing three items.

Check back soon!

Problem 37

True or False? $\mathrm{O}(\mathrm{N})$ is called linear time.

Check back soon!
01:07

Problem 38

True or False? $\mathrm{O}(\mathrm{N})$ is called log time.

Aman Gupta
Aman Gupta
Numerade Educator

Problem 39

True or False? $\mathrm{O}\left(\mathrm{N}^* \mathrm{~N}\right)$ is called quadratic time.

Check back soon!
01:29

Problem 40

True or False? O(1) is called constant time.

Bryan Lynn
Bryan Lynn
Numerade Educator
01:39

Problem 41

True or False? An algorithm that has complexity $\mathrm{O}(\log \mathrm{N})$ is always faster than one that has $\mathrm{O}(\mathrm{N})$ complexity.

Ma. Theresa  Alin
Ma. Theresa Alin
Numerade Educator