Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C++ For Dummies (2004) [eng].pdf
Скачиваний:
84
Добавлен:
16.08.2013
Размер:
8.09 Mб
Скачать

98

Part II: Becoming a Functional C++ Programmer

This program sums values entered by the user

Terminate the loop by entering a negative number

Enter next number: 1

Enter next number: 2

Enter next number: 3

Enter next number: -1

The value of the array is:

0:1

1:2

2:3

The sum is 6

Press any key to continue . . .

Just to keep nonprogrammers guessing, the term iterate means to tra­ verse through a set of objects such as an array. Programmers say that the sumArray() function iterates through the array. In a similar fashion, I “get irate” when my dog iterates from one piece of furniture to another.

Initializing an array

A local variable does not start life with a valid value, not even the value 0. Said another way, a local variable contains garbage until you actually store something in it. Locally declared arrays are the same — each element con­ tains garbage until you actually assign something to it. You should initialize local variables when you declare them. This rule is even truer for arrays. It is far too easy to access uninitialized array elements thinking that they are valid values.

Fortunately, a small array may be initialized at the time it is declared. The fol­ lowing code snippet demonstrates how this is done:

float floatArray[5] = {0.0, 1.0, 2.0, 3.0, 4.0};

This initializes floatArray[0] to 0, floatArray[1] to 1, floatArray[2] to 2, and so on.

The number of initialization constants can determine the size of the array. For example, you could have determined that floatArray has five elements just by counting the values within the braces. C++ can count as well (here’s at least one thing C++ can do for itself).

The following declaration is identical to the preceding one.

float floatArray[] = {0.0, 1.0, 2.0, 3.0, 4.0};

Chapter 7: Storing Sequences in Arrays

99

You may initialize all the elements in an array to a common value by listing only that value. For example, the following initializes all 25 locations in floatArray to 1.0.

float floatArray[25] = {1.0};

Accessing too far into an array

Mathematicians start counting arrays with 1. Most program languages start with an offset of 1 as well. C++ arrays begin counting at 0. The first member of a C++ array is valueArray[0]. That makes the last element of a 128-integer array integerArray[127] and not integerArray[128].

Unfortunately for the programmer, C++ does not check to see whether the index you are using is within the range of the array. C++ is perfectly happy giving you access to integerArray[200]. Our yard is only 128 integers long that’s 72 integers into someone else’s yard. No telling who lives there and what he’s storing at that location. Reading from integerArray[200] will return some unknown and unpredictable value. Writing to that location gen­ erates unpredictable results. It may do nothing — the house may be aban­ doned and the yard unused. On the other hand, it might overwrite some data, thereby confusing the neighbor and making the program act in a seemingly random fashion. Or it might crash the program.

The most common wrong way to access an array is to read or write loca­ tion integerArray[128]. This is one integer beyond the end of the array. Although it’s only one element beyond the end of the array, reading or writ­ ing this location is just as dangerous as using any other incorrect address.

Using arrays

On the surface, the ArrayDemo program doesn’t do anything more than our earlier, non-array-based programs did. True, this version can replay its input by displaying the set of input numbers before calculating their sum, but this feature hardly seems earth shattering.

Yet, the ability to redisplay the input values hints at a significant advantage to using arrays. Arrays allow the program to process a series of numbers multiple times. The main program was able to pass the array of input values to displayArray() for display and then repass the same numbers to sumArray() for addition.