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;
True or False: An out of bounds reference to an array is syntactically legal.
True.
However, you shouldn’t do this as it will result in run-time errors.
True or False:
int list1 = list2;
This will copy the contents of the array list1 into array list2 if they are the same size.
False.
You must copy array contents element-by-element. This is called a DEEP COPY.
A very simple for loop can accomplish this:
for (int i = 0; i < 5; i++)
list1[i] = list2[i];
Is it common to pass the array size explicitly as an additional parameter in a function?
Yes.
The reason is because arrays lose their size information when passed to functions, so having a size parameter in the function helps to make sure the array performs operations accordingly within the function.
You can also use a sentinel value, such as ‘\0’ for character arrays, to mark the end of the array.
Does C++ allow direct return of an entire array from a function?
No.
You’ll have to use pointers to do so indirectly (covered later).
True or False: It is possible to call functions with array values as parameters.
True.
What is the formula for calculating the address of a particular row and column intersection in a 2D matrix array?
address = StartingAddress + ((row_number * number_of_columns) + column_number) * type_size
If you try to print out an array using only its name, what will happen?
It will print out the memory address of the starting address of the array (i.e. 0 for a 1D array or 0,0 for a 2D array.)