MOD 2_ZYBOOKS 2_STRUCTS Flashcards

(25 cards)

1
Q

The ____ construct defines a new type, which can be used to declare a variable with subitems.

A

Struct

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

VIEW: A struct enables creating a variable with data members.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you define a new struct type?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Each struct subitem is called a ____ ____. For a declared variable, each struct data member can be accessed using “.”, known as a ____ ____ operator

A

data member, member access

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens when you assign a variable of a struct type to another such variable?

A

Assigning a variable of a struct type to another such variable automatically assigns each corresponding data member.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Can a struct be used to return multiple values?

A

Yes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

VIEW: Using a struct that is returned from a function; the struct’s data members are copied upon return.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

T/F: A variable of a struct type CANNOT be a function parameter.

A

False, it can.

And just like other types, a pass by value parameter would copy the item, while a pass by reference parameter would not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A ____ is an ordered list of items of a given data type.

A

Vector

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Each item in a vector is called an ____.

A

Element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

A programmer must include the statement #include ____ at the top of the file when planning to use vectors.

A

<vector>
</vector>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you declare a vector?

A

vector<dataType> vectorName(numElements);</dataType>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Terminology note: { } are ____. < > are ____ ____.

A

Braces, angle brackets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

In a vector access, the number in .at() parentheses is called the ____ of the corresponding element.

A

Index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

VIEW: A vector declaration creates multiple variables in memory, each accessible using .at()

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

A powerful aspect of vectors is that the ____ is an expression.

(Give an example)

A

Index

Ex: userNums.at(i) uses the value held in the int variable i as the index. As such, a vector is useful to easily lookup the Nth item in a list.

17
Q

A vector’s index must be what type?

It cannot be what type?

A

Unsigned integer type

Cannot be a floating-point type.

18
Q

VIEW: Vector’s ith element can be directly accessed using .at(i - 1): Oldest people program.

19
Q

A key advantage of vectors becomes evident when used in conjunction with ____.

20
Q

A vector’s ____ function returns the number of vector elements.

21
Q

VIEW: Vectors combined with loops are powerful together: User-entered numbers.

22
Q

A vector’s elements are automatically initialized to ____s during the vector declaration.

23
Q

Can a vector’s elements be initialized to another single value besides 0?

Give an example.

A

Yes

Ex: vector<int> myVector(3, -1); creates a vector named myVector with three elements, each with value -1.</int>

24
Q

How can you initialize each vector element with different values?

A

A programmer may initialize each vector element with different values by specifying the initial values in braces {} separated by commas.

Ex: vector<int> carSales = {5, 7, 11}; creates a vector of three integer elements initialized with values 5, 7, and 11.</int>

Such vector declaration and initialization does not require specifying the vector size, because the vector’s size is automatically set to the number of elements within the braces. For a larger vector, initialization may be done by first declaring the vector, and then using a loop to assign vector elements.

25
VIEW: Common error: Forgetting to include