SH 2
SH 2
Given this structure type definition, what will be the output produced by the following code?
ShoeType shoe1, shoe2;
shoe1.style =’A’;
shoe1.price = 9.99;
cout << shoe1.style << " $" << shoe1.price << endl;
shoe2 = shoe1;
shoe2.price = shoe2.price/9;
cout << shoe2.style << " $" << shoe2.price << endl;
int main( )
{
Stuff x;
//other code
}
declare x to have this structure type. Initialize the members of x, memberB and memberC, to the
values 1 and 2, respectively.
(Note: This requests an initialization, not an assignment of the members.)
4) Write a definition for a structure type for records consisting of a person’s wage rate, accrued vacation
(which is some whole number of days), and status (which is either hourly or salaried). Represent
the status as one of the two char values ’H’ and ’S’. Call the type EmployeeRecord.
1
5) Here is an initialization of a structure type. Tell what happens with each initialization.
struct Date
{
int month;
int day;
int year;
};
6) Write a structure definition for a type named StockRecord that has two member variables, one
named shoeInfo of the type ShoeType given in Problem (1) and one named arrivalDate of type
Date given in Problem (5). Then, declare a variable of type StockRecord and write a statement
that will set the year of the arrival date to 2006.
7) 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 allow the user to input the values for the other one.
Then display both numbers. Interaction with the program should 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
8) 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 allows
the user to 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
Create a structure called Volume that uses three variables of type Distance 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.
Dr. Abdelrahman Morsi