Chapter 11 Flashcards
- What is a primitive data type?
- A data type that is built into the C++ language, such as int, char, float, etc.
- Does a structure declaration cause a structure variable to be created?
No
- Both arrays and structures are capable of storing multiple values. What is the difference between an array and a structure?
- The elements of an array must all be of the same data type. The members of a structure may be of different data types.
- Look at the following structure declaration.
struct Point
{
int x;
int y;
};
Write statements that
A) define a Point structure variable named center
B) assign 12 to the x member of center
C) assign 7 to the y member of center
D) display the contents of the x and y members of center
A) Point center;
B) center.x = 12;
C) center.y = 7;
D) cout «_space;center.x «_space;endl;
cout «_space;center.y «_space;endl;
- Look at the following structure declaration.
struct FullName
{
string lastName;
string middleName;
string firstName;
};
Write statements that
A) Define a FullName structure variable named info
B) Assign your last, middle, and first name to the members of the info variable
C) Display the contents of the members of the info variable
A) FullName info;
B) info.lastName = “Smith”;
info.middleName = “Bart”;
info.firstName = “William”;
C) cout «_space;info.lastName «_space;endl;
cout «_space;info.middleName «_space;endl;
cout «_space;info.firstName «_space;endl;
- Look at the following code.
struct PartData
{
string partName;
int idNumber;
};
PartData inventory[100];
Write a statement that displays the contents of the partName member of element 49 of the inventory array.
- cout «_space;inventory[49].partName «_space;endl;
- Look at the following code.
struct Town
{
string townName;
string countyName;
double population;
double elevation;
};
Town t = { “Canton”, “Haywood”, 9478 };
A) What value is stored in t.townName ?
B) What value is stored in t.countyName ?
C) What value is stored in t.population ?
D) What value is stored in t.elevation ?
A) “Canton”
B) “Haywood”
C) 9478
D) uninitialized
structure Rectangle
{
int length;
int width;
};
Rectangle *r = nullptr
Write statements that
A) Dynamically allocate a Rectangle structure variable and use r to point to it.
B) Assign 10 to the structure’s length member and 14 to the structure’s width member.
A) r = new Rectangle;
B) r->length = 10;
r->width = 14;
- What is the difference between a union and a structure?
- All the members of a union occupy the same area of memory, whereas the members of a structure have their own memory locations.
- Look at the following code.
union Values
{
int ivalue;
double dvalue;
};
Values v;
Assuming that an int uses four bytes and a double uses eight bytes, how much memory does the variable v use?
8 bytes
- What will the following code display?
enum { POODLE, BOXER, TERRIER };
cout «_space;POODLE «_space;” “ «_space;BOXER «_space;” “ «_space;TERRIER «_space;endl;
0 1 2
- Look at the following declaration.
enum Person { BILL, JOHN, CLAIRE, BOB };
Person p;
Indicate whether each of the following statements or expressions is valid or invalid.
A) p = BOB;
B) p ++ ;
C) BILL > BOB
D) p = 0;
E) int x = BILL;
F) p = static_cast<Person>(3);
G) cout << CLAIRE << endl;</Person>
A) valid
B) invalid
C) valid
D) invalid
E) valid
F) valid
G) valid
- Before a structure variable can be created, the structure must be _________.
declared
- The _________ is the name of the structure type.
tag
- The variables declared inside a structure declaration are called _________.
members
- A(n) _________ is required after the closing brace of a structure declaration.
semicolon
- In the definition of a structure variable, the _________ is placed before the variable name, just like the data type of a regular variable is placed before its name.
tag
- The _________ operator allows you to access structure members.
dot
- The structure Car is declared as follows:
struct Car
{
string carMake;
string carModel;
int yearModel;
double cost;
};
Write a definition statement that defines a Car structure variable initialized with the following data:
Make: Ford
Model: Mustang
Year Model: 1968
Cost: $20,000
- Car hotRod = {“Ford”, “Mustang”, 1968, 20000};
- Define an array of 25 of the Car structure variables
struct Car
{
string carMake;
string carModel;
int yearModel;
double cost;
};
Write a definition statement that defines a Car structure variable initialized with the following data:
Make: Ford
Model: Mustang
Year Model: 1968
Cost: $20,000
const int SIZE = 25;
Car collection[SIZE];
- Define an array of 35 of the Car structure variables. Initialize the first three elements with the following data:
Make Model Year Cost
Ford Taurus 1997 $ 21,000
Honda Accord 1992 $ 11,000
Lamborghini Countach 1997 $200,000
const int SIZE = 35;
Car forSale[SIZE] = {
{“Ford”, “Taurus”, 1997, 21000},
{“Honda”, “Accord”, 1992, 11000},
{“Lamborghini”, “Countach”, 1997, 200000} };
- Write a loop that will step through the array you defined in Question 21, displaying the contents of each element.
for (int x = 0; x < SIZE; x++)
{
cout «_space;forSale[x].carMake «_space;endl;
cout «_space;forSale[x].carModel «_space;endl;
cout «_space;forSale[x].yearModel «_space;endl;
cout «_space;forSale[x].cost «_space;endl «_space;endl;
}
- Declare a structure named TempScale, with the following members:
fahrenheit: a double
centigrade: a double
Next, declare a structure named Reading, with the following members:
windSpeed: an int
humidity: a double
temperature: a TempScale structure variable
Next define a Reading structure variable.
struct TempScale
{
double fahrenheit;
double centigrade;
};
struct Reading
{
int windSpeed;
double humidity;
tempScale temperature;
};
Reading today;
- Write statements that will store the following data in the variable you defined in
Question 23.
Wind Speed: 37 mph
Humidity: 32%
Fahrenheit temperature: 32 degrees
Centigrade temperature: 0 degrees
today.windSpeed = 37;
today.humidity = .32;
today.temperature.fahrenheit = 32;
today.temperature.centigrade = 0;
- Write a function called showReading . It should accept a Reading structure variable (see Question 23) as its argument. The function should display the contents of the variable on the screen.
void showReading(Reading values)
{
cout «_space;“Wind speed: “ «_space;values.windSpeed «_space;endl;
cout «_space;“Humidity: “ «_space;values.humidity «_space;endl;
cout «_space;“Fahrenheit temperature: “ «
values.temperature.fahrenheit «_space;endl;
cout «_space;“Centigrade temperature: “ «
values.temperature.centigrade «_space;endl;
}
- Write a function called findReading . It should use a Reading structure reference variable (see Question 23) as its parameter. The function should ask the user to enter values for each member of the structure.
void findReading(Reading &r)
{
cout «_space;“Enter the wind speed: “;
cin»_space; r.windSpeed;
cout «_space;“Enter the humidity: “;
cin»_space; r.humidity;
cout «_space;“Enter the fahrenheit temperature: “;
cin»_space; r.temperature.fahrenheit;
cout «_space;“Enter the centigrade temperature: “;
cin»_space; r.temperature.centigrade;
}