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
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 indexThe 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:
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, exam_scores, ..., 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:
int a; float b; char s; int i = 0;
a = 13; a = 8 * a; b = 10.0; printf("The value of b is %f\n", b); scanf("%c", &s); 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 100In 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];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.
for (i = 0; i < MAX; i++) ex[i] = 0;