C++
Chapter 4 - Arrays

Chapter 4 - Arrays

An array is contiguous segment of memory, which is divided into equal size pieces. These pieces are called elements. An array is randomly addressable by the index of the element. Every Array has a 0th element and has a maximum element of the size or the array-1. This characteristic gives rise to the statement that arrays are zero based.

Example:

Index Data Element Element Size
  0   [ 99 ]      4 bytes
 
1   [ 0 ]       4 bytes
  2   [ 0 ]       4 bytes
  3   [ 0 ]       4 bytes
  4   [ 0 ]       4 bytes

                                             20 Total Bytes 


Array Navigation

Arrays can be navigated in sequence or by random index addressing. Sequential navigation is achieved though repetitive control structures such as "for" or "while".

Example of Sequential Addressing:

Example of Traversing a Double Dimension Array:

Example of Random Addressing:


Declaring and Initializing Arrays

Arrays can be declared with any data type. Arrays should always be initialized to ensure a known state of the object within the array. Arrays declared to contain class based object are initialized through the eyes of the class’s constructor. Arrays of native data types can be initialized using the assignment (=) operator . Arrays of structures can be initialized using the memset function. The size of an array must be declared as a constant.

Example of Declaring and Initializing Arrays


Passing Arrays to Functions

Arrays are passed to functions by using the name of the array, not the brackets. The name of the array is the pointer to the begining of the memory segment. As such an array can be specified by int[] or int *. Passing an array to a function is always performed by a pass by reference or the four bytes for the pointer to the beginning of the array memory segment.

Example Passing Array to Function:

void

Show_String ( char s[] )
{
  cout << s << endl;
}

void
Show_String2 ( char * s )
{
  cout << s << endl;
}

char string[] = {"String to pass to function"};

std::string = "ANSI C++ String class to pass to function";

Show_String ( string );

Show_String2 (string);


Sorting and Searching Arrays

The section covers sorting and searching arrays.  The two basic methods for searching are linear and binary.  Linear searching starts at the head of the array and searches the whole array inorder until the desired data is found.    At worst case the performance of the search is of order "n", O(n), where n is the number of elements in the array.  Binary searching is of Order Log2(n) and is much faster than linear searching but one must know the size of the space to search.  The search algorithm divides the search space in two and search one half.   If the desired value is not found then the remaining search space is divided into two and the process is repeated until the desired item is found.

This chapter gives and example of Bubble Sort.  This sorting alogrithm is at worst case when the array is reverse order O(n2) and at best case when the array is already in order O(n Log(n)).

Some other search types to consider are: ( The Art of Computer Programming, Volume 3, Knuth)

Sorting and Search are in many aspects type independent task.  They are perfect for function template implentation.  Example of Template Functions for Sorting


04/12/00 03:51 PM