ARRAYS Flashcards
Question: What is an array?
Answer: An array is a data structure consisting of an ordered set of elements of common type that are stored contiguously in memory. Contiguous storage is storage without any gaps. An array definition takes the form: “type identifier [ size ];”. Type is the type of each element, identifier is the array’s name, the brackets [ ] identify the data structure as an array, and size specifies the number of elements in the array.
Question: What is the purpose of using an array in programming?
Answer: The purpose of using an array in programming is to process large collections of data efficiently. When variables share the same type and are stored contiguously in memory, structured data can be processed efficiently and the programming of tasks performed on structured data can be simplified considerably.
Question: How can we define an array of 8 grades in C language?
Answer: To define an array of 8 grades in C language, we can use the following statement: “int grade[8];”. This statement allocates contiguous storage in RAM for an array named grade that consists of 8 int elements.
Question: How can we specify the size of an array using #define or const int?
Answer: We can specify the size of an array using #define or const int. For example, we can use “#define NGRADES 8” or “const int NGRADES = 8” to specify the size of an array. Then we can define an array of NGRADES elements as “int grade[NGRADES];”. This coding style facilitates modifiability, as we need to change the size in only one place if we want to modify the array size.
What is the syntax for accessing a specific element of an array?
To refer to a specific element, we write the array name followed by bracket notation around the element’s index. The syntax is: identifier[index].
What is the index numbering for arrays?
Index numbering starts at 0 and extends to one less than the number of elements in the array.
How do we iterate through all elements of an array to display them?
We can use a for loop to iterate through all elements of an array. For example, to display all elements of grade, we can use the following code:
for (int i = 0; i < NGRADES; i++)
{
printf(“%d” , grade[i]);
}
Why is it important to check array bounds?
C compilers do not introduce code that checks whether an element’s index is within the bounds of its array. It is our responsibility as programmers to ensure that our code does not include index values that point to elements outside the memory allocated for an array.
What is the syntax for initializing an array?
We can initialize an array when we define it using the following syntax: type identifier[size] = {value, …, value}. For example, to initialize grade, we can use the following code:
int grade[NGRADES] = {10, 9, 10, 8, 7, 9, 8, 10};
What happens when we initialize an array with fewer values than its size?
If we initialize an array with fewer values than its size, C compilers fill the uninitialized elements with zero values. For example, int grade[NGRADES] = {0}; initializes all 8 elements of grade to zero.
What happens if we specify a size that is less than the number of initial values when initializing an array?
Specifying a size that is less than the number of initial values generates a syntax error.
What are parallel arrays?
Parallel arrays are two arrays where the elements at the same index hold data that are related to the same entity. One array holds the key data, while the other holds the value data.
How do we access elements in parallel arrays?
The elements of parallel arrays with the same index make up the fields of a single record of information. Once we find the index of the element that matches the specified key, we also have the index of the corresponding value.
What is a string?
A string is a character array with a special property: a terminator element follows the last meaningful character in the string.
What is the null terminator in a string?
The null terminator is a special character that identifies the end of a string. It is represented by the escape sequence ‘\0’. The null terminator has the value 0 on any host platform and all of its bits are 0’s.