Exercises

  1. 14.5 (Fill in the Blanks) Fill in the blanks in each of the following:

    1. Computers store large amounts of data on secondary storage devices as              .

    2. The standard stream objects declared by header <iostream> are              ,              ,               and              .

    3. ostream member function               repositions the file-position pointer in a file.

    4.               is the default file-open mode for an ofstream.

    5. istream member function              repositions the file-position pointer in a file.

  2. 14.6 (File Matching) Exercise 14.3 asked you to write a series of single statements. Actually, these statements form the core of an important type of file-processing program, namely, a file-matching program. In commercial data processing, it’s common to have several files in each application system. In an accounts-receivable system, for example, there is generally a master file containing detailed information about each customer, such as the customer’s name, address, telephone number, outstanding balance, credit limit, discount terms, contract arrangements and, possibly, a condensed history of recent purchases and cash payments.

    As transactions occur (e.g., sales are made and cash payments arrive), they’re entered into a file. At the end of each business period (a month for some companies, a week for others and a day in some cases), the file of transactions (called trans.dat in Exercise 14.3) is applied to the master file (called oldmast.dat in Exercise 14.3), thus updating each account’s record of purchases and payments. During an updating run, the master file is rewritten as a new file (newmast.dat), which is then used at the end of the next business period to begin the updating process again.

    File-matching programs must deal with certain problems that do not exist in single-file programs. For example, a match does not always occur. A customer on the master file might not have made any purchases or cash payments in the current business period, and therefore no record for this customer will appear on the transaction file. Similarly, a customer who did make some purchases or cash payments may have just moved to this community, and the company may not have had a chance to create a master record for this customer.

    Use the statements from Exercise 14.3 as a basis for writing a complete file-matching accounts-receivable program. Use the account number on each file as the record key for matching purposes. Assume that each file is a sequential file with records stored in increasing order by account number.

    When a match occurs (i.e., records with the same account number appear on both the master and transaction files), add the dollar amount on the transaction file to the current balance on the master file, and write the newmast.dat record. (Assume purchases are indicated by positive amounts on the transaction file and payments are indicated by negative amounts.) When there is a master record for a particular account but no corresponding transaction record, merely write the master record to newmast.dat. When there is a transaction record but no corresponding master record, print the error message "Unmatched transaction record for account number ..." (fill in the account number from the transaction record).

  3. 14.7 (File Matching Test Data) After writing the program of Exercise 14.6, write a simple program to create some test data for checking out the program. Use the sample account data in Figs. 14.1414.15

    Fig. 14.14 Master file data.

    Master file Account number Name Balance
    100 Alan Jones 348.17
    300 Mary Smith 27.19
    500 Sam Sharp 0.00
    700 Suzy Green –14.22

    Fig. 14.15 Transaction file data.

    Transaction file Account number Transaction amount
    100 27.14
    300 62.11
    400 100.56
    900 82.17
  4. 14.8 (File-Matching Test) Run the program of Exercise 14.6, using the files of test data created in Exercise 14.7. Print the new master file. Check that the accounts have been updated correctly.

  5. 14.9 (File-Matching Enhancement) It’s common to have several transaction records with the same record key, because a particular customer might make several purchases and cash payments during a business period. Rewrite your accounts-receivable file-matching program of Exercise 14.6 to provide for the possibility of handling several transaction records with the same record key. Modify the test data of Exercise 14.7 to include the additional transaction records in Fig. 14.16.

    Fig. 14.16 Transaction records to add.

    Account number Dollar amount
    300 83.89
    700 80.78
    700 1.53
  6. 14.10 Write a series of statements that accomplish each of the following. Assume that we’ve defined class Person that contains the private data members

    
    char lastName[15];
    char firstName[10];
    int age;
    int id;
    

    and public member functions

    
    // accessor functions for id
    void setId(int);
    int getId() const;
    
    // accessor functions for lastName
    void setLastName(const string&);
    string getLastName() const;
    
    // accessor functions for firstName
    void setFirstName(const string&);
    string getFirstName() const;
    
    // accessor functions for age
    void setAge(int);
    int getAge() const;
    

    Also assume that any random-access files have been opened properly.

    1. Initialize nameage.dat with 100 records that store values lastName ="unassigned", firstName = "" and age = 0.

    2. Input 10 last names, first names and ages, and write them to the file.

    3. Update a record that already contains information. If the record does not contain information, inform the user "No info".

    4. Delete a record that contains information by reinitializing that particular record.

  7. 14.11 (Hardware Inventory) You own a hardware store and need to keep an inventory that can tell you what different tools you have, how many of each you have on hand and the cost of each one. Write a program that initializes the random-access file hardware.dat to 100 empty records, lets you input the data concerning each tool, enables you to list all your tools, lets you delete a record for a tool that you no longer have and lets you update any information in the file. The tool identification number should be the record number. Use the information in Fig. 14.17 to start your file.

    Fig. 14.17 Hardware inventory records.

    Record # Tool name Quantity Cost
    3 Electric sander 7 57.98
    17 Hammer 76 11.99
    24 Jig saw 21 11.00
    39 Lawn mower 3 79.50
    56 Power saw 18 99.99
    68 Screwdriver 106 6.99
    77 Sledge hammer 11 21.50
    83 Wrench 34 7.50
  8. 14.12 (Telephone-Number Word Generator) Standard telephone keypads contain the digits 0 through 9. The numbers 2 through 9 each have three letters associated with them, as is indicated by the following table:

    Fig. 14.18 Telephone digit-to-letter mappings.

    Digit Letter
    2 A B C
    3 D E F
    4 G H I
    5 J K L
    6 M N O
    7 P Q R S
    8 T U V
    9 W X Y Z

    Many people find it difficult to memorize phone numbers, so they use the correspondence between digits and letters to develop seven-letter words that correspond to their phone numbers. For example, a person whose telephone number is 686-2377 might use the correspondence indicated in the above table to develop the seven-letter word “NUMBERS.”

    Businesses frequently attempt to get telephone numbers that are easy for their clients to remember. If a business can advertise a simple word for its customers to dial, then no doubt the business will receive a few more calls. Each seven-letter word corresponds to exactly one seven-digit telephone number. The restaurant wishing to increase its take-home business could surely do so with the number 825-3688 (i.e., “TAKEOUT”). Each seven-digit phone number corresponds to many separate seven-letter words. Unfortunately, most of these represent unrecognizable juxtapositions of letters. It’s possible, however, that the owner of a barber shop would be pleased to know that the shop’s telephone number, 424-7288, corresponds to “HAIRCUT.” A veterinarian with the phone number 738-2273 would be happy to know that the number corresponds to “PETCARE.”

    Write a program that, given a seven-digit number, writes to a file every possible seven-letter word corresponding to that number. There are 2187 (3 to the seventh power) such words. Avoid phone numbers with the digits 0 and 1.

  9. 14.13 (sizeof Operator) Write a program that uses the sizeof operator to determine the sizes in bytes of the various data types on your computer system. Write the results to the file data-size.dat, so that you may print the results later. The results should be displayed in two-column format with the type name in the left column and the size of the type in the right column, as in Fig. 14.19. [Note: The sizes of the built-in data types on your computer might differ from those listed here.]

    Fig. 14.19 Sample output for Exercise 14.13.

    Alternate View
    
    char                    1
    unsigned char           1
    short int               2
    unsigned short int      2
    int                     4
    unsigned int            4
    long int                4
    unsigned long int       4
    long long int           8
    unsigned long long int  8
    float                   4
    double                  8
    long double            10
    
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset