ARRAYS Flashcards

1
Q

Question: What is an array?

A

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.

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

Question: What is the purpose of using an array in programming?

A

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.

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

Question: How can we define an array of 8 grades in C language?

A

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.

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

Question: How can we specify the size of an array using #define or const int?

A

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.

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

What is the syntax for accessing a specific element of an array?

A

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].

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

What is the index numbering for arrays?

A

Index numbering starts at 0 and extends to one less than the number of elements in the array.

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

How do we iterate through all elements of an array to display them?

A

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]);
}

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

Why is it important to check array bounds?

A

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.

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

What is the syntax for initializing an array?

A

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};

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

What happens when we initialize an array with fewer values than its size?

A

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.

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

What happens if we specify a size that is less than the number of initial values when initializing an array?

A

Specifying a size that is less than the number of initial values generates a syntax error.

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

What are parallel arrays?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do we access elements in parallel arrays?

A

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.

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

What is a string?

A

A string is a character array with a special property: a terminator element follows the last meaningful character in the string.

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

What is the null terminator in a string?

A

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.

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

Where does the null terminator element occupy in the collating sequences?

A

The null terminator occupies the first position in the ASCII and EBCDIC collating sequences.

17
Q

What is the value of the index that identifies the null terminator element in a string?

A

The value of the index identifying the null terminator element is the number of meaningful characters in the string.

18
Q

How many memory locations does a string occupy?

A

The number of memory locations occupied by a string is one more than the number of meaningful characters in the string, because the null terminator element follows the last meaningful character.

19
Q

Question: What is the purpose of allocating memory for one additional byte when defining a character array in C?

A

Answer: The additional byte is used to store the null terminator, which indicates the end of the character string.

20
Q

Question: What is the value of the null terminator in the ASCII and EBCDIC collating sequences?

A

Answer: The null terminator has the value 0 on any host platform (in its collating sequence).

21
Q

Question: What is the index of the null terminator element in a character string?

A

Answer: The value of the index identifying the null terminator element is the number of meaningful characters in the string.

22
Q

Question: How do we print the contents of a character string in C using printf()?

A

Answer: We use the “%s” conversion specifier and the address of the start of the character string to send its contents to standard output.

23
Q

Question: What is a parallel array in C and how is it used?

A

Answer: A parallel array is a way to store tabular information through two arrays that are parallel because the elements at the same index hold data that are related to the same entity. In C, once we find the index of the element that matches the specified key, we also have the index of the corresponding value for that element.