Previous: A Compound Data Type --- array
Up: A Compound Data Type --- array
Next: 7.1.2 Character Strings as Arrays
Previous Page: A Compound Data Type --- array
Next Page: 7.1.2 Character Strings as Arrays

## 7.1.1 Declaring Arrays

Let us consider the task of reading and printing a list of exam scores.

LIST0: Read and store a list of exam scores and then print it.

Since we are required to store the entire list of scores before printing it, we will use an array hold the data. Successive elements of the list will be stored in successive elements of the array. We will use a counter to indicate the next available position in the array. Such a counter is called an index into the array. Here is an algorithm for our task:

```initialize the index to the beginning of the array
while there are more data items
read a score and store in array at the current index
increment index
set another counter, count = index - the number of items in the array
traverse the array: for  each index starting at the beginning to count
print the array element at index```
The algorithm reads exam scores and stores them in successive elements of an array. Once the list is stored in an array, the algorithm traverses the array, i.e. accesses successive elements, and prints them. A count of items read in is kept and the traversal continues until that count is reached.

We can implement the above algorithm in a C program as shown in Figure 7.1.

Before explaining this code, here is a sample session generated by executing this program:

• ***List of Exam Scores***
• Type scores, EOF to quit
• 67
• 75
• 82
• 69
• '136D
• ***Exam Scores***
• 67
• 75
• 82
• 69

Referring to the code in Figure 7.1, the program first declares an array, exam_scores[MAX], of type integer. This declaration allocates a contiguous block of memory for objects of integer type as shown in Figure 7.2. The macro, MAX, in square brackets gives the size of the array, i.e. the number of elements this compound data structure is to contain. The name of the array, exam_scores, refers to the entire collection of MAX integer cells. Individual objects in the array may be accessed by specifying the name of the array and the index, or element number, of the object; a process called indexing. In C, the elements in the array are numbered from 0 to MAX - 1. So, the elements of the array are referred to as exam_scores[0], exam_scores[1], ..., exam_scores[MAX - 1], where the index of each element is placed in square brackets. These index specifiers are sometimes called subsctipts, analogous to the mathematical expression a. These indexed or subscripted array expressions are the names of each object in the array and may be used just like any other variable name.

In the code, the while loop reads a score into the variable, n, places it in the array by assigning it to exam_scores[index], and increments index. The loop is terminated either when index reaches MAX (indicating a full array) or when returns EOF, indicating the end of the data. We could have also read each data item directly into exam_scores[index] by writing scanf() as follows:

`scanf("%d", &exam_scores[index])`
We choose to separate reading an item and storing it in the array because the use of the increment operator, ++, for index is clearer if reading and storing of data items are separated.

Once the data items are read and stored in the array, a count of items read is stored in the variable count. The list is then printed using a for loop. The array is traversed from element 0 to element count - 1, printing each element in turn.

From the above example, we have seen how we can declare a variable to be of the compound data type, array, how data can be stored in the elements of the array, and subsequently accessed. More formally, the syntax for an array declaration is:

-specifieridentifier>[<size>];
where the <type-specifier> may be any scalar or derived data type; and the <size> must evaluate, at compile time, to an unsigned integer. Such a declaration allocates a contiguous block of memory for objects of the specified type. The data type for each object in the block is specified by the <type-specifier>, and the number of objects in the block is given by |sf <size> as seen in Figure 7.2. As stated above, the index values for all arrays in C must start with 0 and end with the highest index, which is one less than the size of the array. The subscripting expression with the syntax:
[<expression>]
is the name of one element object and may be used like any other variable name. The subscript, <expression> must evaluate, at run time, to an integer. Examples include:

```int a[10];
float b[20];
char s[100];
int  i = 0;

a[3] = 13;
a[5] = 8 * a[3];
b[6] = 10.0;
printf("The value of b[6] is %f\n", b[6]);
scanf("%c", &s[7]);
c[i] = c[i+1];```

Through the remainder of this chapter, we will use the following symbolic constants for many of our examples:

```/* File: araydef.h */
#define MAX 20
#define SIZE 100```
In programming with arrays, we frequently need to initialize the elements. Here is a loop that traverses an array and initializes the array elements to zero:
```int i, ex[MAX];

for (i = 0; i < MAX; i++)
ex[i] = 0;```
The loop assigns zero to ex[i] until i becomes MAX, at which point it terminates and the array elements are all initialized to zero. One precaution to programmers using arrays is that C does not check if the index used as a subscript is within the size of the declared array, leaving such checks as the programmer's responsibility. Failure to do so can, and probably will result in catastrophe.

Previous: A Compound Data Type --- array
Up: A Compound Data Type --- array
Next: 7.1.2 Character Strings as Arrays
Previous Page: A Compound Data Type --- array
Next Page: 7.1.2 Character Strings as Arrays

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:56:22 HST 1994