5 - Arrays Flashcards
What is an array?
An array behaves like a list of variables with a uniform naming mechanism that can be declared in a single line of simple code.
Declare an array, score, which consists of five variables type int.
int score [5];
What is the name of the number in brackets in the array?
score [5];
Index
Define: size of array.
The number of indexed variables in the array is called the declared size of the array, or sometimes simply the size of the array.
What is the base type of the array?
The base type refers to the type of variables stored in the array. All indexed variables in one array are of the same type.
What is a common loop used with arrays?
The for-loop is ideally suited to array manipulations.
The indexes of an array always starts with…
0
The indexes of an array always ends with …
size - 1
Can variables be used to declare array size?
You may be tempted to use a variable for the array size., but not all compilers will allow this. For the sake of portability you should not do so. Use constants, not variables. Or use dynamic arrays or declare an array size that is of the largest size the program could possibly need.
What is the most common programming error made when using arrays?
Attempting to reference a nonexistent array index.
What happens if you reference a nonexistent array index?
Unfortunately, most compilers will allow you to do this. As long as the program has the permission to read or write from that memory location, it will do so if given the instructions to.
The most common programming error made when using arrays is attempting to reference a nonexistent array index. When does this usually happen?
At the first or last iteration of a loop that processes the array.
How do you initialize the array children of size three along with its declaration?
int children [3] = {2, 12, 1}
int b[] = {5, 12, 11}
Is this legal?
Yes. The array is now set to size 3.
We have an array called a.
void showTheWorld ( const int a [], int sizeOfa)
Why const?
In a complicated function you might write code that inadvertently changes one or more of the values stored in the array even though the array should not be changed at all. It is a good idea to use const.