Chapter 11 - Structures

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

The following program skeleton, when complete, asks the user to enter these data about his or her favorite movie: Name of movie Name of the movie's director Name of the movie's producer The year the movie was released Complete the program by declaring the structure that holds this data, defining a structure variable, and writing the individual statements necessary. #include <iostream> using namespace std; // Write the structure declaration here to hold the movie data. int main() { // define the structure variable here. cout << "Enter the following data about your\n"; cout << "favorite movie.\n"; cout << "name: "; // Write a statement here that lets the user enter the // name of a favorite movie. Store the name in the // structure variable. cout << "Director: "; // Write a statement here that lets the user enter the // name of the movie's director. Store the name in the // structure variable. cout << "Producer: "; // Write a statement here that lets the user enter the // name of the movie's producer. Store the name in the // structure variable. cout << "Year of release: "; // Write a statement here that lets the user enter the // year the movie was released. Store the year in the // structure variable. cout << "Here is data on your favorite movie:\n"; // Write statements here that display the data. // just entered into the structure variable. return 0; }

#include <iostream> #include <string> using namespace std; struct Movie { string name; string director; string producer; string year; }; int main() { Movie favorite; cout << "Enter the following information about your\n"; cout << "favorite movie.\n"; cout << "Name: "; getline(cin, favorite.name); cout << "Director: "; getline(cin, favorite.director); cout << "Producer: "; getline(cin, favorite.producer); cout << "Year of release: "; getline(cin, favorite.year); cout << "Here is information on your favorite movie:\n"; cout << "Name: " << favorite.name << endl; cout << "Director: " << favorite.director << endl; cout << "Producer: " << favorite.producer << endl; cout << "Year of release: " << favorite.year << endl; return 0; }

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

0 1 2

What will the following code display? enum { HOBBIT, ELF = 7, DRAGON }; cout << HOBBIT << " " << ELF << " " << DRAGON << endl;

0 7 8

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 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 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. 0

Look at the following structure declaration: struct FullName { string lastName; string middleName; string firstName; }; A. Define FullName structure variable named info B. Assign your last, middle, and first name tot he members of the info variable C. Display contents of the members of the info variable

A. FullName info; B. info.lastName = "Haque"; info.middleName ="Mohammed"; info.firstName = "Naveed"; C. cout <<"Last name: " << info.lastName << endl; cout <<"Middle name: "<<info.middleName<<endl;cout<<"First name: "<<info.firstName<<endl;

Look at the following structure declaration struct Point { int x; int y; }; A. Write statements that define a Point structure variable named center B. Assigned 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. Pointer center; B. center.x = 12; C. center.y = 7; D. cout <<"X: "<<center.x<<"Y: "<<center.y<<endl;

Look at the following code 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;

A. Valid B. Invalid C. Invalid D. Invalid E. Valid F. Valid G. Valid

Look at the following code: 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;

Write a definition statement for a variable of the structure you declared in Question 11.1. Initialize the members with the following data: Account Number: ACZ42137-B12-7 Account Balance: $4512.59 Interest Rate: 4% Average Monthly Balance: $4217.07

Account savings = {"ACZ42137-B12-7", 4512.59, 0.04, 4217.07 };

(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.

Both arrays and structures are capable of storing multiple values. What is the difference between an array and a structure?

Arrays can store ONE data type while structures can store multiple data types

struct Rectangle { int length; int width; }; Assume rptr is a pointer to a Rectangle structure. Which of the expressions, A, B, or C, is equivalent to the following expression: rptr->width A) *rptr.width B) (*rptr).width C) rptr.(*width)

B

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};

What is a primitive data type?

Data types that are defined as a basic part of the language

The ___ operator allows you to access structure members.

Dot(.)

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

False

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

False

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

False

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

False

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

False, a structure pointer is used in place of the dot operator to refer data of a structure. The structure pointer can use it to automatically dereference the structure pointer on its left.

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

False, must be written carInfo[4].modelMust be 4 because it starts at 0, not 1

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

False, they have equal precedence

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

False, they must be outputted by each member separately

(Find error) struct ThreeVals { int a, b, c; }; int main () {TwoVals s, *sptr = nullptr; 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.

Does the enumerated data type declared in Checkpoint Question 11.22 have a name, or is it anonymous?

It is anonymous.

(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, the variable must be created inside the main function after the structure has been declared.

struct Product { string description; // Product description int partNum; // Part number double cost; // Product cost }; Write a definition for an array of 100 Product structures. Do not initialize the array.

Product items[100];

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; }

struct Rectangle { int length; int width; }; Write the definition of a pointer to a Rectangle structure.

Rectangle *rptr;

struct Rectangle { int length; int width; }; Write a function that returns a Rectangle structure. The function should store the user's input in the members of the structure before returning it.

Rectangle getRect() { Rectangle r; cout << "Width: "; cin >> r.width; cout << "length: "; cin >> r.length; return r; }

(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;

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.

Structure name or Tag

The ___ is the name of the structure type.

Tag

Will the following code cause an error, or will it compile without any errors? If it causes an error, rewrite it so it compiles. enum Color { RED, GREEN, BLUE }; Color c; c = 0;

The code will not compile. The assignment statement should be written as: c = static_cast<Color>(0);

Will the following code cause an error, or will it compile without any errors? If it causes an error, rewrite it so it compiles. enum Color { RED, GREEN, BLUE }; Color c = RED; c++;

The code will not compile. The statement c++ should be written as: c = static_cast<Color>(c + 1);

(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.

The code will not compile. The statement c++ should be written as: c = static_cast<Color>(c + 1);

The integers 0, 1, and 2.

(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.

Write the definition for an array of 50 of the ThreeTypes structures you declared in Question 11.16.

ThreeTypes Items[50];

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

True

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

True

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

True

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

True

(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.

True

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

True, as long as you do not skip elements

What will the following code display? enum Letters { Z, Y, X }; if (Z > X) cout << "Z is greater than X. \n"; else cout << "Z is not greater than X. \n";

Z is not greater than X.

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 { 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 << "The contents of partName of the 49th element is: << inventory[49].partName << endl;

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

declared

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 };

struct Product { string description; // Product description int partNum; // Part number double cost; // Product cost }; Write a loop that will display the contents of the entire array you created in Question 11.4.

for (int x = 0; x < 100; x++) { cout << items[x].description << endl; cout << items[x].partNum << endl; cout << items[x].cost << endl << endl; }

struct Product { string description; // Product description int partNum; // Part number double cost; // Product cost }; Write a loop that will step through the entire array you defined in Question 11.4, setting all the product descriptions to an empty string, all part numbers to zero, and all costs to zero.

for (int x = 0; x < 100; x++) { items[x].description = ""; items[x].partNum = 0; items[x].cost = 0; }

Write a loop that stores the character 'A' in all the elements of the array you defined in Question 11.17.

for (int x = 0; x < 50; x++) items[x].letter = 'A';

Write a loop that stores the floating point value 2.37 in all the elements of the array you defined in Question 11.17.

for (int x = 0; x < 50; x++) items[x].real = 2.37;

Write a loop that stores the integer 10 in all the elements of the array you defined in Question 11.17.

for (int x = 0; x < 50; x++) items[x].whole = 10;

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; }

struct Product { string description; // Product description int partNum; // Part number double cost; // Product cost }; Write the statements that will store the following data in the first element of the array you defined in Question 11.4: Description: Claw hammer Part Number: 547 Part Cost: $8.29

items[0].description = "Claw Hammer"; items[0].partNum = 547; items[0].cost = 8.29;

Write statements that store the following data in the variable you defined in Question 11.9: City: Tupelo Miles: 375 Meters: 603,375

place.city = "Tupelo"; place.distance.miles= 375; place.distance.meters = 603375;

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;

Write a structure declaration to hold the following data about a savings account: Account Number ( string object) Account Balance ( double ) Interest Rate ( double ) Average Monthly Balance ( double )

struct Account { string acctNum; double acctBal; double intRate; double avgBal; };

Write a structure declaration named Destination , with the following members: city, a string object distance, a Measurement structure (declared in Question 11.8) Also define a variable of this structure type.

struct Destination { string city; Measurement distance; }; Destination place;

Write a structure declaration named Measurement , with the following members: miles, an integer meters, a long integer

struct Measurement { int miles; long meters; };

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;

The variables declared inside a structure declaration are called ___.

structure members

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;

Declare a union named ThreeTypes with the following members: letter: A character whole: An integer real: A double

union ThreeTypes { char letter; int whole; double real };

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; }

struct Rectangle { int length; int width; }; Write a function that uses a Rectangle structure reference variable as its parameter and stores the user's input in the structure's members.

void getRect(Rectangle &r) { cout << "Width: "; cin >> r.width; cout << "length: "; cin >> r.length; }

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; }

struct Rectangle { int length; int width; }; Write a function that accepts a Rectangle structure as its argument and displays the structure's contents on the screen.

void showRect(Rectangle r) { cout << r.length << endl; cout << r.width << endl; }


Set pelajaran terkait

Pathogenic Cocci of Medical importance

View Set

FBLA-Intro to Information Technology

View Set

MKTG - CH. 9 +10, 11 + 12 - QUIZZES

View Set

Історія соціології

View Set

Ethical Hacking - C701 CEH Certified Ethical Hacker Practice Exams, Fourth Edition 1/2

View Set

Human Anatomy and Physiology- Chapter 10 Chapter Test

View Set

Powers -- are they delegated, reserved, or concurrent?

View Set