QUESTION 01 · Array Basics
What is an array?
2 marksAnswer
An array is a fixed-length sequence of elements of one type, stored contiguously.
- Each element is selected using an index.
- For an array of length n, valid indices are 0 through n - 1.
QUESTION 01 · Array Basics
An array is a fixed-length sequence of elements of one type, stored contiguously.
QUESTION 02 · Array Basics
An array stores several values of one element type under one name. An index identifies each element.
int marks[3] = {70, 80, 90}; creates three int elements. Their names are marks[0], marks[1] and marks[2]. The number in brackets in a declaration gives the element count, not the last index. All elements belong to the same array and occupy contiguous storage.
Arrays are useful for marks, measurements and sorted lists. A direct valid element access takes O(1) time in the usual random-access model. Processing all n elements takes O(n). Storing n elements requires O(n) space, while a fixed number of counters adds O(1) auxiliary space. A single indexed access does not sort or search the whole array.
An array of length 3 has no element at index 3. Access outside its bounds has undefined behaviour; it is not guaranteed to display zero or a predictable extra value.
Related questions in the arrays category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.
Write an algorithm to sum all elements in an array and find its time complexity.
Paper P02 · page 2What is an array? Describe declaration and initialisation of one- and two-dimensional arrays.
Paper P03 · page 1