WORKING WITH ARRAYS


Sometimes it's convenient to link similar data. One big advantage is that you gain the convenience of being able to use one variable name to refer to multiple items. Without arrays, if you needed 100 variables of a certain type, you would have to name each one of them separately.

But more importantly, you can perform all kinds of optimized searches and organizational tricks when you can rely on the fact that your data is contiguous—that is, predictably located in a linear sequence in memory.

Hint 

An array is an indexed List of data. The first piece of data stored in an array is assigned an index position of 0. The next piece of data is assigned an index position of I, and so on. You can reference any data stored in the array based on its index position.

An array is a repeating sequence of a single data type with a fixed size. Each item of the array is known as an element. You declare the type of data that will make up each element and the name you want to call it. Then you specify how many elements you want in brackets after the array name. Upon declaration, each element is reserved in as many adjacent blocks of memory as you've requested.

Defining Arrays

The format for a basic array takes the size (number of elements) in brackets:

 <data type>     <variable name>[<size> 

Following is an example of an array:

 Int16 intCodeArr[50];       //A list of 50 integers 

When you declare an array, it is as if you've declared n number of variables of the same type, where n is the quantity you've specified in brackets. Because computers count beginning with zero, your list of elements begins at zero and runs to just one under the number you choose. So if you declare an array often elements, it's important to know that the list starts at zero and extends to nine.

Just as any variable is filled with garbage if you fail to initialize it, arrays, too, must be initialized. You have two options for doing so: You can either initialize an array when you first declare it, or you can initialize each element.

The former method is practical for relatively small arrays. Small essentially means as many elements as you have the patience to specify by hand. To initialize an array when you declare it, you assign it to a comma-delimited list of values surrounded by braces. Each value is specified as a literal:

 Double dblUserAverage[5] = { 0.25, 0.17, 0.22, 0.98, 0.77 }; 

If you use this method, you can omit the number of elements in the array. Visual C++ automatically determines the size of the array when you compile the program. Here is an example:

 Int16 intCodeCombo[] = { 7,2,3,4,5,6,9,7,1 }; 

As you can see, the manual initialization method can become cumbersome quickly. Alternatively, if you know that you want all the elements initialized to the same value, you can specify a single literal value within the braces regardless of the size of the array:

 Double initialDistances[100] = { 0.0 }; 

Unfortunately, using braces works only when you're declaring the array. A common way of initializing an array, which you can use anywhere after your array is declared, is to use an iterator. An iterator is a fancy word for a counter, which you commonly use to keep track of values to control a loop. By using an iterator within the brackets of your array, you can traverse every element:

 Int32 highScores[kNumHighScores]; for( int i; i < kNumHighScores; i++ )     highScores[i] = 0; 

This code example loops through an array of high scores and sets each element to zero. Because you have fine control over an iterator, you can move up or down the elements of the array in any way you want. This allows you to assign a different value to every fifth element, for example, if you need to. Or you can assign values based on a formula.

Trap 

Take care that when you access the elements of an array, you stay within the bounds of the elements you've declared. If you try to access areas outside of this range, you might get nasty results. Your array is a location in memory. If you try to write values to or access areas that your program doesn't have permission to touch, you might cause insidious, hard-to-find bugs or even mysterious crashes in your program.




Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
Microsoft Visual C++ 2005 Express Edition Programming for the Absolute Beginner 2006
ISBN: 735615381
EAN: N/A
Year: 2005
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net