C++ Deck 7 Flashcards
What is a composite variable?
Variables that are capable of storing multiple values under the same name with some way to tell them apart.
What is the simplest form of composite data type?
An array
What is an array?
Data structures that allow the storage of multiple elements of the same type in contiguous memory locations.
What does it mean that an array is indexed?
The elements contained in the array are numbered (starting at 0)
True or False: Every element in an array must be the same data type.
True
What is the formula for calculating the memory address of an index in an array?
Address = starting address + (size of element type * index number)
Which of the following is NOT a property of an array:
a. Same Data Type
b. Continuous in Memory
c. Indexed (Beginning at Zero)
d. Variable size upon declaration
d. Variable size upon declaration
What is the index range of an array?
0 to size-1
What is the syntax of declaring an array?
typeName variableName[size];
The size in the brackets must be an integer literal or a constant variable. The compiler uses the size to determine how much space to allocate (i.e. how many bytes)
True or False: When initializing an array using set notation, it is permissible to exceed the number of values allocated. C++ will adjust the size.
False
A c-style string is implemented as an array of type char that ends with what?
The null character
char name[7] = “Connie”;
(note that it’s 6 characters long, but room has been left for the null character to be the 7th index. This is REQUIRED.)
It’s the equivalent of:
char name[7] = {‘C’, ‘o’, ‘n’,’n’,’i’,’e’,’\0’};
Is this a valid way to declare and initialize an array:
int list[] = {1, 3, 5, 7, 9};
Yes, C++ will make the array size just large enough to capture the initialized data.
Which of these is legal initialization of an array:
int list[5] = {1, 2};
int nums[3] = {1, 2, 3, 4};
int nums[3] = {1, 2, 3, 4}; is illegal because the number of elements being initialized (4) is greater than the size (3).
int list[5] = {1,2}; has fewer elements initialized than the size, but the uninitialized elements are simply defaulted to 0: {1, 2, 0, 0, 0}
True or False: You can initialize an array with a for loop, and this is a common way to do so.
True.
int numList[10];
int i;
for (i=0; i < 10; i++)
numList[i] = i*2;
The above loop initializes numList to {0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
In this array:
int numList[10];
How would one assign a value of 6 to the 8th element of the array?
numList[7] = 6;