Chapter 11 Structures C++

¡Supera tus tareas y exámenes ahora con Quizwiz!

What will the following code display? enum { POODLE, BOXER, TERRIER }; cout << POODLE << " " << BOXER << " " << TERRIER << endl;

0 1 2

Look at the following code: struct Town { char townName[51]; char countyName[51]; 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?

7. A) "Canton" B) "Haywood" C) 9478 D) uninitialized

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

Look at the following code: structure Rectangle { int length; int width; }; Rectangle *r; 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.

8. A) r = new Rectangle; B) r->length = 10; r->width = 14;

What is a primitive data type?

A data type that is built into the C++ language, such as int, char, float, etc.

Look at the following statement. enum Color { RED, ORANGE, GREEN, BLUE }; A) What is the name of the data type declared by this statement? B) What are the enumerators for this type? C) Write a statement that defines a variable of this type and initializes it with a valid value.

A) Color B) RED, ORANGE, GREEN, and BLUE C) Color myColor = BLUE;

Look at the following structure declaration: struct FullName { char lastName[26]; char middleName[26]; char firstName[26]; }; 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 << info.lastName << endl; cout << info.middleName << endl; cout << info.firstName << endl;

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 << center.x << endl; cout << center.y << endl;

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(3); G) cout << CLAIRE << endl;

A) valid B) invalid C) valid D) invalid E) valid F) valid G) valid

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.

(Find error) struct FourVals { int a, b, c, d; }; int main () { FourVals nums = {1, 2, , 4}; return 0; }

An initializer cannot be skipped before the end of the initialization list.

(Find error) #include <iostream> using namespace std; union Compound { int x; float y; }; int main() { Compound u; u.x = 1000; cout << u.y << endl; return 0; }

Both x and y cannot be meaningfully used at the same time.

The structure Car is declared as follows: struct Car { char carMake[20]; char carModel[20]; 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};

(T/F) A structure variable may not be a member of another structure.

F

(T/F) All the members of a union may be used simultaneously.

F

(T/F) An entire structure may not be passed to a function as an argument.

F

(T/F) In a union, all the members are stored in different memory locations.

F

(T/F) Structure variables may not be initialized.

F

(T/F) The contents of a structure variable can be displayed by passing the structure variable to the cout object.

F

(T/F) The following expression refers to the element 5 in the array carInfo: carInfo.model[5]

F

(T/F) The indirection operator has higher precedence than the dot operator.

F

(T/F) The structure pointer operator does not automatically deference the structure pointer on its left.

F

(T/F) You may not define pointers to unions.

F

(T/F) You may skip members in a structure's initialization list.

F

(Find error) struct ThreeVals { int a, b, c; }; int main () { TwoVals s, *sptr; sptr = &s; *sptr.a = 1; return 0; }

In the declaration of the structure variables in main, TwoVals is used as the type instead of ThreeVals. The statement that reads *sptr.A = 1; should read sptr->A = 1;

(Find error) #include <iostream> using namespace std; struct ThreeVals { int a, b, c; }; int main() { ThreeVals vals = {1, 2, 3}; cout << vals << endl; return 0; }

In the definition of vals, TwoVals is used as the type instead of ThreeVals. An entire struct cannot be sent to cout.

(Find error) struct TwoVals { int a, b; }; int main () { TwoVals.a = 10; TwoVals.b = 20; return 0; }

No structure variable has been declared. TwoVals is the structure tag.

Does a structure declaration cause a structure variable to be created?

No.

Write a function called getReading, which returns a Reading structure- struct TempScale { double fahrenheit; double centigrade; }; struct Reading { int windSpeed; double humidity; tempScale temperature; }; Reading today; The function should ask the user to enter values for each member of a Reading structure, then return the structure.

Reading getReading() { Reading local; cout << "Enter the following values:\n"; cout << "Wind speed: "; cin >> local.windSpeed; cout << "Humidity: "; cin >> local.humidity; cout << "Fahrenheit temperature: "; cin >> local.temperature.fahrenheit; cout << "Centigrade temperature: "; cin >> local.temperature.centigrade; return local; }

(Find error) #include <iostream> using namespace std; struct TwoVals { int a = 5; int b = 10; }; int main() { TwoVals v; cout << v.a << " " << v.b; return 0; }

Structure members cannot be initialized in the structure definition.

(Find error) struct TwoVals { int a = 5; int b = 10; }; int main() { TwoVals varray[10]; varray.a[0] = 1; return 0; }

Structure members cannot be initialized in the structure definition. The statement varray.a[0] = 1 should read: varray[0].a = 1;

(T/F) A function may return a structure.

T

(T/F) A semicolon is required after the closing brace of a structure or union declaration.

T

(T/F) A structure declaration does not define a variable.

T

(T/F) A structure member variable may be passed to a function as an argument.

T

(T/F) An anonymous union has no name.

T

(T/F) An array of structures may be initialized.

T

(T/F) If an anonymous union is defined globally (outside all functions), it must be declared static.

T

(T/F) In a structure variable's initialization list, you do not have to provide initializers for all the members.

T

(T/F) When a function returns a structure, it is always necessary for the function to have a local structure variable to hold the member values that are to be returned.

T

(T/F) You may define arrays of unions.

T

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.

(Find error) struct TwoVals { int a; int b; }; TwoVals getVals() { TwoVals.a = TwoVals.b = 0; }

The function must define a variable of the TwoVals structure. The variable, then, should be used in the assignment statement.

(Find error) #include <iostream> using namespace std; struct names { char first[20]; char last[20]; }; int main () { names customer = "Smith", "Orley"; cout << names.first << endl; cout << names.last << endl; return 0; }

The initialization list of the customer variable must be enclosed in braces.

(Find error) struct Values { char name[30]; int age; }

The semicolon is missing after the closing brace.

(Find error) struct { int x; float y; };

The structure declaration has no tag.

Define an array of 25 of the Car structure variables- struct Car { char carMake[20]; char carModel[20]; int yearModel; double cost; };

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- Ford, Honda, Lamborghini Model- Taurus, Accord, Countach Year- 1997, 1992, 1997 Cost- $21,000; $11,000; $11,000

const int SIZE = 35; Car forSale[SIZE] = { {"Ford", "Taurus", 1997, 21000}, {"Honda", "Accord", 1992, 11000}, {"Lamborghini", "Countach", 1997, 200000} };

Look at the following code: struct PartData { char partName[51]; int idNumber; }; PartData inventory[100]; Write a statement that displays the contents of the partName member of element 49 of the inventory array

cout << inventory[49].partName << endl;

Before a structure variable can be created, the structure must be _________.

declared

The _________ operator allows you to access structure members.

dot

A pet store sells dogs, cats, birds, and hamsters. Write a declaration for an anonymous enumerated data type that can represent the types of pets the store sells.

enum Pets { DOGS, CATS, BIRDS, HAMSTERS };

Write a loop that will step through the array- const int SIZE = 35; Car forSale[SIZE] = { {"Ford", "Taurus", 1997, 21000}, {"Honda", "Accord", 1992, 11000}, {"Lamborghini", "Countach", 1997, 200000} }; Displaying the contents of each element.

for (int x = 0; x < SIZE; x++) { cout << forSale[x].carMake << endl; cout << forSale[x].carModel << endl; cout << forSale[x].yearModel << endl; cout << forSale[x].cost << endl << endl; }

The variables declared inside a structure declaration are called _________.

members

Write a statement that stores the number 452 in the num member of the anonymous union- union { char alpha; int num; long bigNum; float real; };

num = 452;

Rewrite the following statement using the structure pointer operator: (*rptr).windSpeed = 50;

rptr->WindSpeed = 50;

A(n) _________ is required after the closing brace of a structure declaration.

semicolon

Rewrite the following statement using the structure pointer operator: *(*strPtr).num = 10;

strPtr->num = 10;

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;

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 _________ is the name of the structure type.

tag

Write statements that will store the following data in the variable- struct TempScale { double fahrenheit; double centigrade; }; struct Reading { int windSpeed; double humidity; tempScale temperature; }; Reading today; 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 the declaration of an anonymous union with the same members as the union- union Items { char alpha; int num; long bigNum; float real; }; Items x;

union { char alpha; int num; long bigNum; float real; };

Write the declaration of a union called Items with the following members: alpha a character num an integer bigNum a long integer real a float Next, write the definition of an Items union variable.

union Items { char alpha; int num; long bigNum; float real; }; Items x;

Write a function called findReading. It should use a Reading structure reference variable- struct TempScale { double fahrenheit; double centigrade; }; struct Reading { int windSpeed; double humidity; tempScale temperature; }; Reading today; as its parameter. The function should ask the user to enter values for each member of the structure.

void findReading(Reading &r) { cout << "Enter the wind speed: "; cin >> r.windSpeed; cout << "Enter the humidity: "; cin >> r.humidity; cout << "Enter the fahrenheit temperature: "; cin >> r.temperature.fahrenheit; cout << "Enter the centigrade temperature: "; cin >> r.temperature.centigrade; }

Write a function called recordReading. It should use a Reading structure pointer variable- struct TempScale { double fahrenheit; double centigrade; }; struct Reading { int windSpeed; double humidity; tempScale temperature; }; Reading today; as its parameter. The function should ask the user to enter values for each member of the structure pointed to by the parameter.

void recordReading(Reading *r) { cout << "Enter the wind speed: "; cin >> r->windSpeed; cout << "Enter the humidity: "; cin >> r->humidity; cout << "Enter the fahrenheit temperature: "; cin >> r->temperature.fahrenheit; cout << "Enter the centigrade temperature: "; cin >> r->temperature.centigrade; }

Write a function called showReading. It should accept a Reading structure variable- struct TempScale { double fahrenheit; double centigrade; }; struct Reading { int windSpeed; double humidity; tempScale temperature; }; Reading today; as its argument. The function should display the contents of the variable on the screen.

void showReading(Reading values) { cout << "Wind speed: " << values.windSpeed << endl; cout << "Humidity: " << values.humidity << endl; cout << "Fahrenheit temperature: " << values.temperature.fahrenheit << endl; cout << "Centigrade temperature: " << values.temperature.centigrade << endl; }


Conjuntos de estudio relacionados

Chapter 16 Fluid and electrolytes

View Set

Chapter 11 - Marginal Cost and Average Cost

View Set

Sociology 170: Population Problems

View Set

La Belle Dame Sans Merci: A Ballad by John Keats (English Lit A2)

View Set

Chapter 13: Promoting Patient Comfort During Labor and Birth

View Set