[previous_group] [previous] [up] * [next_group]

Arrays as Databases

We have been using arrays to collect together related information, all of the same type, into one data structure, with a single name. We can use a collection of arrays to represent a datadase of information.

For example, consider the stock program again. For each stock in a portfolio, we had its id number, number of shares, whole, numerator and denominator values for the stock price, and the calculated value of the stock. A protfolio consists of a number of values for each of these variables. In our previous programs, we read and processed each stock individually; one at a time. We can now declare a set of arrays to store an entire portfolio in the program:

   int id[MAX_STOCKS],       /* the stock id numbers      */
   shares[MAX_STOCKS],       /* the number of shares of stock */
   whole[MAX_STOCKS],        /* the whole dollar price of stock */
   numerator[MAX_STOCKS],denominator[MAX_STOCKS];  /*  the fractional  */
                                                      /* price of stock  */
   float value[MAX_STOCKS];  /* the value of a stock  */

This allocates six arrays:

[up] to Overview.