Chapter 4
Structures
EXERCISES:
Answers to these questions can be found in Appendix G.
1. A structure brings together a group of
a. items of the same data type.
b. related data items.
c. integers with user-defined names.
d. variables.
2. True or false: A structure and a class use similar syntax.
3. The closing brace of a structure is followed by a __________.
4. Write a structure specification that includes three variables—all of type int—called hrs,
mins, and secs. Call this structure time.
5. True or false: A structure definition creates space in memory for a variable.
Chapter 4 156
05 3087 CH04 11/29/01 2:23 PM Page 156
6. When accessing a structure member, the identifier to the left of the dot operator is the
name of
a. a structure member.
b. a structure tag.
c. a structure variable.
d. the keyword struct.
7. Write a statement that sets the hrs member of the time2 structure variable equal to 11.
8. If you have three variables defined to be of type struct time, and this structure contains
three int members, how many bytes of memory do the variables use together?
9. Write a definition that initializes the members of time1—which is a variable of type
struct time, as defined in Question 4—to hrs = 11, mins = 10, secs = 59.
10. True or false: You can assign one structure variable to another, provided they are of the
same type.
11. Write a statement that sets the variable temp equal to the paw member of the dogs member of the fido variable.
12. An enumeration brings together a group of
a. items of different data types.
b. related data variables.
c. integers with user-defined names.
d. constant values.
13. Write a statement that declares an enumeration called players with the values B1, B2,
SS, B3, RF, CF, LF, P, and C.
14. Assuming the enum type players as declared in Question 13, define two variables joe
and tom, and assign them the values LF and P, respectively.
15. Assuming the statements of Questions 13 and 14, state whether each of the following
statements is legal.
a. joe = QB;
b. tom = SS;
c. LF = tom;
d. difference = joe - tom;
16. The first three enumerators of an enum type are normally represented by the values
_________, _________, and _________.
17. Write a statement that declares an enumeration called speeds with the enumerators
obsolete, single, and album. Give these three names the integer values 78, 45, and 33.
Structures
4
STRUCTURES
157
05 3087 CH04 11/29/01 2:23 PM Page 157
18. State the reason that
enum isWord{ NO, YES };
is better than
enum isWord{ YES, NO };
Answers to the starred exercises can be found in Appendix G.
Question No 1 :
A phone number, such as (212) 767-8900, can be thought of as having three parts: the area code (212), the exchange (767), and the number (8900). Write a program that uses a structure to store these three parts of a phone number separately. Call the structure phone. Create two structure variables of type phone. Initialize one, and have the user input a number for the other one. Then display both numbers. The interchange might look like this:
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
Your number is (415) 555-1212
Question No 2 :
A point on the two-dimensional plane can be represented by two numbers: an x coordinate and a y coordinate. For example, (4,5) represents a point 4 units to the right of the vertical axis, and 5 units up from the horizontal axis. The sum of two points can be defined as a new point whose x coordinate is the sum of the x coordinates of the two points, and whose y coordinate is the sum of the y coordinates.
Write a program that uses a structure called point to model a point. Define three points, and have the user input values to two of them. Then set the third point equal to the sum of the other two, and display the value of the new point. Interaction with the program might look like this:
Enter coordinates for p1: 3 4
Enter coordinates for p2: 5 7
Coordinates of p1+p2 are: 8, 11
Question No 3 :
Create a structure called Volume that uses three variables of type Distance (from the ENGLSTRC example) to model the volume of a room. Initialize a variable of type Volume to specific dimensions, then calculate the volume it represents, and print out the result. To calculate the volume, convert each dimension from a Distance variable to a variable of type float representing feet and fractions of a foot, and then multiply the resulting three numbers.
Question No 4 :
Create a structure called employee that contains two members: an employee number (type int) and the employee’s compensation (in dollars; type float). Ask the user to fill in this data for three employees, store it in three variables of type struct employee, and then display the information for each employee.
Question No 5 :
Create a structure of type date that contains three members: the month, the day of the month, and the year, all of type int. (Or use day-month-year order if you prefer.) Have the user enter a date in the format 12/31/2001, store it in a variable of type struct date, then retrieve the values from the variable and print them out in the same format
Question No 6 :
We said earlier that C++ I/O statements don’t automatically understand the data types of enumerations. Instead, the (>>) and (<<) operators think of such variables simply as integers. You can overcome this limitation by using switch statements to translate between the user’s way of expressing an enumerated variable and the actual values of the enumerated variable. For example, imagine an enumerated type with values that indicate an employee type within an organization:
enum etype { laborer, secretary, manager, accountant, executive, researcher };
Write a program that first allows the user to specify a type by entering its first letter (‘l’, ‘s’, ‘m’, and so on), then stores the type chosen as a value of a variable of type enum etype, and finally displays the complete word for this type.
Enter employee type (first letter only)
laborer, secretary, manager,
accountant, executive, researcher): a Employee type is accountant.
You’ll probably need two switch statements: one for input and one for output.
0 Comments