Chapter 8: Review Questions and Exercises

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

Questions 28-30 are for students who have covered Chapter 7 on classes and structures. These questions use the following Car structure declaration. struct Car { string make, model; int year; double cost; // Constructors Car() { make = model = ""; year = cost = 0; } Car(string mk, string md, int yr, double c) { make = mk; model = md; year = yr; cost = c; } }; 30. Write a loop that will step through the array you defined in question 29, displaying the contents of each element.

// Loop to iterate through the array of structure // objects. for (int i = 0; i <= 3; i++) { cout << "forSale[i].Make << endl; cout << "forSale[i].Model << endl; cout << "forSale[i].Year << endl; cout << "forSale[i].Cost << endl; }

4. Subscript numbering in C++ always starts at _________.

0

22. Given the following array definition: int values[5] = { 4, 7, 6, 8, 2 }; What does the following statement display? cout << values[4] << " " << (values[2] + values[3]) << " " << ++values[1] << endl;

2 14 8

21. Look at the following array definition. int values[10]; A) How many elements does the array have? B) What is the subscript of the first element in the array? C) What is the subscript of the last element in the array? D) If an int uses four bytes of memory, how much memory does the array use?

A) 10 B) 0 C) 9 D) 40 bytes

23. Look at the following array definition. int numbers[5] = { 1, 2, 3 }; A) What value is stored in numbers[2]? B) What value is stored in numbers[4]?

A) 3 B) 0

27. Look at the following array definition. double sales[8][10]; A) How many rows does the array have? B) How many columns does the array have? C) How many elements does the array have? D) Write a statement that stores 3.52 in the last column of the last row in the array.

A) 8 B) 10 C) 80 D) sales[7][9] = 3.52;

10. Look at the following array definition. double amount[5]; A) How many elements does this array hold? B) What can you store in amount[5]?

A) Can hold 5 values B) 64-bit double precision floating point values

25. Assume that vec1 is an existing integer vector. Each of the following statements attempts to create a new integer vector, vec2. Indicate whether each of these statements is legal or illegal. A) vector<int> vec2; B) vector<int> vec2 (10); C) vector<int> vec2 (10, 100); D) vector<int> vec2 (vec1); E) vector<int> vec2 (vec1.size( ));

A) Defined syntax of creating a vector object, this statement defined vec2 as a vector of int. B) Defines as a vector of 10 ints. C) vec2 is defined as a vector of 10 integers. Each element in numbers is initialized to the value 2. D) User can also initialize a vector with the value in another vector. After the execution of the above statement, the vector vec2 will have the same numbers of elements and hold the same set of values of vec1. E)Here, the size() method will return the size of a vector. So, the statement vec1.size() will return the size of vector vec1. The size which is returned by the statement vec1.size() became the argument of vec2. Suppose vec1.size() return size as n, so the statement will become, vector vec2(n); this statement defines as a vector vec2 of n integers.

24. Assume that array1 and array2 are both 25-element integer arrays. Indicate whether each of the following statements is legal or illegal. A) array1 = array2; B) cout << array1; C) cin >> array2;

A) Illegal; as assignment cannot work directly to assign the values into another array. B) Illegal; cout cannot print the elements in two-dimensional arrays without specifying its index value or subscript value. C) Illegal; you need to attach a subscript to array2, you cannot simply put a value into a static array memory position. It would have to be: cin >> array2[0];

Questions 28-30 are for students who have covered Chapter 7 on classes and structures. These questions use the following Car structure declaration. struct Car { string make, model; int year; double cost; // Constructors Car() { make = model = ""; year = cost = 0; } Car(string mk, string md, int yr, double c) { make = mk; model = md; year = yr; cost = c; } }; 28. Define an array named collection that holds 25 Car structures.

Car collection[25];

Questions 28-30 are for students who have covered Chapter 7 on classes and structures. These questions use the following Car structure declaration. struct Car { string make, model; int year; double cost; // Constructors Car() { make = model = ""; year = cost = 0; } Car(string mk, string md, int yr, double c) { make = mk; model = md; year = yr; cost = c; } }; 29. Define an array named forSale that holds 35 Car structures. Initialize the first three elements with the following data: Make Model Year Cost Ford Taurus 2006 $21,000 Honda Accord 2004 $11,000 Jeep Wrangler 2007 $24,000

Car forSale[35] = {Car("Ford", "Taurus", 2006, 21000), {Car("Honda", "Accord", 2004, 11000), {Car("Jeep", "Wrangler", 2007, 24000)};

12. You cannot use the _________ operator to copy data from one array to another in a single statement.

assignment operator

6. C++ has no array _________ checking, which means you can inadvertently store data past the end of an array.

bounds

19. When a two-dimensional array is passed to a function, the number of _________ must be specified.

columns

2. The size declarator must be a(n) _________ with a value greater than _________.

constant integer, zero

(EXTRA) To allow an array of structures or an array of objects to be initialized, the struct or class declaration should include a(n) _________.

constructor

(EXTRA) To print out all elements of a two-dimensional array you would normally use a(n) _________ loop.

for

7. Starting values for the elements of an array may be specified with a(n) _________ list.

initialization

9. If the size declarator of an array definition is omitted, C++ counts the number of items in the _________ to determine how large the array should be.

initialization list

15. A(n) _________ array is like several arrays of the same type put together.

multidimensional

14. To pass an array to a function, pass the _________ of the array.

name

16. It's best to think of a two-dimensional array as having _________ and _________.

rows, columns

26. How do you establish a parallel relationship between two or more arrays?

same size

18. When initializing a two-dimensional array, it helps to enclose each row's initialization list in _________.

set of braces {}

1. The _________ indicates the number of elements, or values, an array can hold.

size declarator

5. The number inside the brackets of an array definition is the _________, but the number inside an array's brackets in an assignment statement, or any other statement that works with the contents of the array, is the _________.

size declarator, subscript

11. By using the same _________ for multiple arrays, you can build relationships between the data stored in the arrays. These arrays are referred to as parallel arrays.

subscript

3. Each element of an array is accessed and indexed by a number known as a(n) _________.

subscript

20. When you pass the name of an array as an argument to a function, you are actually passing _________________.

the address of the array

17. To define a two-dimensional array, _________ size declarators are required.

two

13. Arrays are never passed to functions by _________ because there would be too much overhead in copying all the elements.

value

8. If a numeric array is partially initialized, the uninitialized elements will bet set to _________.

zero


Set pelajaran terkait

chapter 5 accounting LS questions

View Set

Chapter 4: Command line interface management

View Set

Pharm II Week 2 Enteral and Parenteral Nutrition/Electrolyte Balance

View Set

CHAPTER III/IV Discrete Mathematics

View Set