Arrays
- C++ stores list of values in arrays. An array is a consecutive group of related memory locations. These locations are related by the fact that they all have the same name and the same type. To refer to a particular location or element within the array, we specify the name of the array and the subscript.
- A subscript may be an integer or an integer expression. Subscript expressions are evaluated to determine the particular element of the array.
- It is important to note the difference when referring to the seventh element of the array as opposed to array element seven. The seventh element has a subscript of 6, while array element seven has a subscript of 7 (actually the eighth element of the array). This is a source of "off-by-one" errors.
- Arrays occupy space in memory. To reserve 100 elements for integer array b and 27 elements for integer array x, the programmer writes int b[ 100 ], c[ 27 ] ;
- An array of type char can be used to store a character string.
- The elements of an array can be initialized: by declaration. By assignment, and by input.
- If there are fewer initializers than elements in the array, the remaining elements are initialized to zero.
- C++ does not prevent referencing elements beyond the bounds of an array.
- A character array can be initialized using a string literal.
- All strings end with the null character ( '\0' ).
- Character arrays can be initialized with character constants in an initializer list.
- Individual characters in a string stored in an array can be accessed directly using array subscript notation.
- To pass an array to a function, the name of the array is passed. To pass a single element of an array to a function, simply pass the name of the array followed by the subscript (contained in square brackets) of the particular element.
- Arrays are passed to functions using simulated call-by-reference--the called functions can modify the element values in the callers' original arrays. The value of the name of the array is the address of the first element of the array. Because the starting address of the array is passed, the called function knows precisely where the array is stored.
- To receive an array argument, the function's parameter list must specify that an array will be received. The size of the array is not required in the array brackets.
- C++ provides the type qualifier const that enables programs to prevent modification of array values in a function. When the const qualifier precedes an array parameter, the elements of the array become constant in the function body, and any attempt to modify an element of the array in the function body is a syntax error.
- An array can be sorted using the bubble-sort technique. Several passes of the array are made. On each pass, successive pairs of elements are compared. If a pair is in order (or the values are identical), it is left as is. If a pair is out of order, the values are swapped. For small arrays, the bubble sort is acceptable, but for larger arrays it is inefficient compared to other more sophisticated sorting algorithms.
- The linear search compares each element of the array with the search key. If the array is not in any particular order, it is just as likely that the value will be found in the first element as the last. On average, therefore, the program will have to compare the search key with half the elements of the array. The linear searching method works well for small arrays and is acceptable for unsorted arrays.
- Binary search eliminates from consideration half the elements in the array after each comparison by locating the middle element of the array and comparing it to the search key. If they are equal, the search key is found and the array subscript of that element is returned. Otherwise the problem is reduced to searching one half of the array.
- In a worst case scenario, searching an array of 1024 elements will take only 10 comparisons using a binary search.
- Arrays may be used to represent tables of values consisting of information arranged in rows and columns. To identify a particular element of a table, two subscripts are specified: The first (by convention) identifies the row in which the element is contained, and the second (by convention) identifies the column in which the element is contained. Tables or arrays that require two subscripts to identify a particular element are called double-subscripted arrays.
- When we receive a single-subscripted array as an argument to a function, the array brackets are empty in the function's parameter list. The size of the first subscript of a multiple-subscripted array is not required either, but all subsequent subscript sized are required. The compiler uses these sizes to determine the locations in memory of elements in multiple-subscripted arrays.
- To pass one row of a double-subscripted array to a function that receives a single-subscripted array, simply pass the name of the array followed by the first subscript.