Previous: 7.5 Array Initializers
Up: 7 Arrays
Next: 7.7 Common Errors
Previous Page: 7.5 Array Initializers
Next Page: 7.7 Common Errors

7.6 Arrays for Databases

We now consider our payroll task that reads input data and calculates pay as before, but the program prints a table of computed pay for all the id's. The algorithm uses arrays to store the data, but is otherwise very similar to our earlier programs: get data, calculate pay, and print results. We will use functions to perform these subtasks. Here are the prototypes:

/* File: payutil.h */
     int getdata(int id[], float hrs[], float rate[], int lim);
     void calcpay(float hrs[], float rate[], float reg[], float over[], int n);
     void printdata(int id[], float hrs[], float rate[],
                                  float reg[], float over[], int n);
The function, getdata() gets the data into the appropriate arrays for id's, hours worked, and rate of pay; returning the number of id's entered by the user. While the arrays id[], hrs[], and rate[] are individual arrays, we make sure that the same value of the array index accesses the data for a given id. For example, id[i] accesses an id number and hrs[i] and rate[i] access hours worked and rate of pay for that id number. In other words, an input data record for each id number resides at the same index in these arrays.oWe can think of this data structure as a table, where the columns are the arrays holding different pieces of information; and the rows are the data for an individual id, as shown in Figure 7.12.

Next, calcpay() calculates and stores regular and overtime pay for each id in arrays, regpay[] and overpay[] (columns), at the same array index as the input data record. Thus, the entire payroll data record for each id number is at a unique index in each of the arrays. Finally, printdata() prints each payroll record, i.e. the input data as well as the calculated regular, overtime, and total pay. We will write getdata(), printdata(), and calcpay() in the file, . The prototypes shown above for these functions are in the file, payutil.h. This header file is included in the program file paytab.c, where main() will reside (see Figure 7.13). We also include the header file, paydef.h which defines the symbolic constants, REG_LIMIT and OT_FACTOR.:

/* paydef.h */
#define REG_LIMIT 40
#define OT_FACTOR 1.5

The program calls getdata() which reads data into the appropriate arrays and stores the returned value (the number of id's) into n. It then calls on calcpay() to calculate the pay for n people, filling in the regpay[] and arrays, and calls printdata() to print the input data and the results for n people. The code for these functions is shown in Figure 7.14.

In the function, getdata(), scanf() is used to read data for the itemsa, using n to count and index the data items in the arrays. We use pointer arithmetic to pass the necessary arguments to scanf(). For example, to read data into id[n], we must pass its address &id[n]. Instead, we pass id + n which is identical to &id[n]. The function, getdata(), reads data for as many id's as possible, returning either when there is no more data (a zero id value) or the arrays are full ( n reaches the limit, lim passed in). If the array limit is reached, an appropriate message is printed and the input is terminated. The function returns the number of id's placed in the arrays. The other functions in the program are straight forward; each index accesses the data record for the id at that index.

As written, terminates input of data when an invalid id ( id <= 0) is entered. An alternative might be to read a data item in a temporary variable first, examine it for validity if desired, and then assign it to an array element. For example:

scanf("%d", &x);
     if (x > 0)
         id[n] = x;
     else
         return n;
Here is a sample session for the program, paytab.c compiled and linked with payutil.c:

Sample Session:



Previous: 7.5 Array Initializers
Up: 7 Arrays
Next: 7.7 Common Errors
Previous Page: 7.5 Array Initializers
Next Page: 7.7 Common Errors

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