3.2 Example Uses of ArraysThe source listing below provides a simple program that illustrates the creation and use of an array based on the Array ADT. Comments are provided to highlight the use of the operator methods. Program Listing
As a second example, suppose you need to read the contents of a text file and count the number of letters occurring in the file with the results printed to the terminal. We know that characters are represented by the ASCII code, which consists of integer values. The letters of the alphabet, both upper- and lowercase, are part of what's known as the printable range of the ASCII code. This includes the ASCII values in the range [32 ... 126] along with some of the codes with smaller values. The latter are known control characters and can include the tab, newline, and form-feed codes. Since all of the letters will have ASCII values less than 127, we can create an array of this size and let each element represent a counter for the corresponding ASCII value. After processing the file, we can traverse over the elements used as counters for the letters of the alphabet and ignore the others. The following program provides a solution to this problem using the Array ADT: Program Listing
Python does not provide the array structure as part of the language itself, but it does include low-level modules that provide access to the diverse set of data types available at the hardware-level, including arrays. The use of these low-level modules to create a hardware array is beyond the scope of this text. Thus, we have included the
|