Section 11d

Adding Data to a One Dimensional Array


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Adding Data to an Array

Visiting Elements within Arrays

One of the processes required of any group or list of data is the traversing process, which is also known as visiting elements within an array. There are some simple, but necessary tasks that need to be implemented with arrays, so traversing, or visiting each element one at a time, is used.

A task mentioned previously is the need to fill the array with some value, such as zero. This is how an array is initialized. For the array given earlier, the process might look like the following.

for( index = 0; index < arraySize; index++ )
{
integerArray[ index ] = 0;
}

There is nothing particularly complicated about traversing an array, and it is easily accomplished using standard loops. This is a pattern with which you should become very familiar; virtually all the times you need to visit every element or some series of elements in an array, it will look much like, or exactly like, this. Shown next are a couple of other ways that the simple array traversal might be implemented. For an array whose values might need to be printed out, the following could be used.

for( index = 0; index < arraySize; index++ )
{
printString( "Item Number " );
printInteger( index + 1 );
printString( COLON_SPACE );
printInteger( integerArray[ index ] );
printEndline();
}

An example result of the above loop, and using the data from the first array sample shown previously, would be as shown below.

Item Number 1: 5
Item Number 2: 3
Item Number 3: 3
.
.
Item Number 10: 4

For an array whose values might be added or averaged, the following code could be used.

int sum = 0;
double average;

for( index = 0; index < arraySize; index++ )
{
sum += integerArray[ index ];
}

average = double ( sum ) / arraySize;

Again, the operation of traversing an array is fairly simple, as long you as properly implement your loop. As mentioned above, since arrays are zero based, you have to start with an index of zero, and traverse up to an index of one less than the number of items (N - 1) to properly access all the data. You can never use the capacity of an array as an index; it is by definition, outside of the array you have created.

Adding Data to Arrays

There are essentially two ways to add data to an array. You can either insert or append the data. Insertion of data is what was done when the value 49 was placed in the sixth array element previously. Insertion means that you place data into the array in a certain element without concern for where the array begins or ends, as you might when you working to sort the data set. Appending is easier; you simply add it to the end of the array.

Inserting Data Values

Insertion of data into an array requires that you overwrite the value in the array location. If the data at that location is important, you may want to manage the insertion process. Consider the following character array, called charArray, that holds an organized set of letters.

Example of array with misspelled word "restaurant" in it

A series of characters is, by definition, a string. However, for this study you can just treat this as an array of characters that happen to spell a word . . . almost. Since the spelling is a little flawed, you might be motivated to insert an 'a' into the eighth element (i.e., at index seven). This would be implemented as shown next.

charArray[ 7 ] = 'a';

After this assignment, the array would now hold the following data.

Array with word "restaurant" in it, and letter 'n' overwritten by letter 'a'

The letter 'a' is now in the correct place. However, the process works like any other variable. If you put a new value in an array element, the old value will be overwritten. Your problem is that you really need to insert the letter into the others without losing the data you have. This is not difficult to do, and is a great exercise for your looping skills. Consider the following pseudo code that does not use a loop at first.

assign the item at index 8 to the element at index 9
assign the item at index 7 to the element at index 8
insert the character at index 7

The program code is little different. Consider the following code lines.

// 't' is placed at index 9
charArray[ 9 ] = charArray[ 8 ];

// 'n' is placed at index 8
charArray[ 8 ] = charArray[ 7 ];

// 'a' is placed at index 7
charArray[ 7 ] = 'a';

Now consider the process with a loop. The array graphic will be used to support the process. The array is shown as it was originally.

Example of array with misspelled word "restaurant" in it

Here is the code that will be implemented.

int counter = 9;
int insertAt = 7;

while( counter > insertAt )
{
charArray[ counter ] = charArray[ counter - 1 ];

counter--;
}

charArray[ insertAt ] = 'a';

Consider the first iteration of the loop. Since counter, which is initially assigned (9), is greater than insertAt (7), the while loop starts. The element at index counter (9) is assigned the value of the element at index counter - 1 (8), and then counter (9) is decremented to 8. The result of this single iteration is shown next. Notice that the tenth element (i.e., at index 9) receives the value of the ninth element (i.e., at index 8 ).

However, the value at index 8 is unchanged, as shown.

The letter 't' has been copied from index location 8 to index location 9

For the next iteration, the while test is implemented, counter (8) is greater than insertAt (7), and the loop continues. The element at index counter (8) is assigned the value of the element at index counter - 1 (7), and then counter is decremented to 7. The result is shown next. The letter ‘n’ has now been placed at element index 8.

The letter 'n' at index location 7 has been copied to index location 8

For the next step, the while test is implemented, but counter (7) is not greater than insertAt (7), so the loop is not implemented. The next step in the code, and after the loop ends, is to assign the value ‘a’ to the element at the insertAt (7) index, so that is implemented. The result is shown next.

The letter 'a' has been inserted, or assigned into, index location 7

The insertion process is as straightforward as counting indices and copying data items. It should be noted here that the above exercise is treated simply as an array of characters. If this were a C-Style string, and the expectation was to be input or output like a string, there may be other complications that would be considered..

Watch this video to see the code and array elements updated together, in a function.

Appending Data Values

The appending process is as easy as adding the character to the index after the last data element. If another test score were to be appended to an array, it would work as shown below. In most appending processes, your array will start with nothing in it. However, to both support the example, and to show you another initialization process, a couple of array initialization formats are shown here.

The first is not useful for the present exercise, but can be used in some instances. The following definition and initialization process creates an array of exactly the number of values that are given to it, and then places them in their locations. The code appears next.

double dArray[] = { 5.5, 6.6, 7.7, 8.8, 9.9 };

Certainly, the element values can still be changed at a later time, but this strategy allows you to start with a set of pre-programmed, also known as hard-coded numbers, if that is helpful for developing the program. A limitation to this format is that you will provided a capacity limited to the number of values you originally placed in the curly braces.

The next example allows you to specify a larger capacity than just the number of values, and is the one that will be used to demonstrate the appending process.

double[] testScores = new double[ MAX_TEST_SCORES ];
testScores[ 0 ] = 92.5;
testScores[ 1 ] = 88.6;
testScores[ 2 ] = 78.9;
testScores[ 3 ] = 89.4;
testScores[ 4 ] = 95.6;

The MAX_TEST_SCORES constant will be set to whatever is known as the maximum number of tests that might be entered. As it stands in this condition, you can observe that there are five test scores in the array. So, if you want to append a value to this array, you simply get the index of the sixth element (i.e., five), and add the value to that position. The array would be initialized as follows.

Array with various floating point numbers in each element

Remember again that those elements of an array that are not specifically initialized – in this case, the elements at index 5 and above – may have garbage in them unless you take some action to change that condition. The appending process consists of no more than adding a value at the index one above the last.

Since it is known that the last value is at index four and the list length at present is five, it is easy to know where to place the appended item. Your list length will always be the index of the next unused data element in an array. Again, this is easy stuff. You just append the item to the list by placing it at the same location as the list length, and then of course you will need to increment the list length, as shown next.

testScore[ listLength ] = 98.6;
listLength++;

The resulting array would now appear as follows, with 98.6 at the sixth element (i.e., index 5).

Array with new 98.6 added at index location 5

Appending is pretty straightforward but you can watch this video to observe the updating process in a function.

Inserting and appending data to arrays will become more important as you learn more about data structures and management. However, it is still important to simply study how to get homogeneous data into a variable that can hold a whole list of that data type. The opportunities to extend your looping skills will continue, and you will have several opportunities to learn about applying standard algorithms to your programming processes. The next thing to learn is how to remove a data item from an array, and you will learn about that in the next topic.