Exercises
17.5 |
Fill in the blanks in each of the following:
- Computers store large amounts of data on secondary storage devices as__________.
- A(n)__________is composed of several fields.
- To facilitate the retrieval of specific records from a file, one field in each record is chosen as a__________.
- The vast majority of information stored in computer systems is stored in__________files.
- A group of related characters that conveys meaning is called a(n)__________.
- The standard stream objects declared by header are__________,__________,__________and__________.
- ostream member function__________outputs a character to the specified stream.
- ostream member function__________is generally used to write data to a randomly accessed file.
- istream member function__________repositions the file-position pointer in a file.
|
17.6 |
State which of the following are true and which are false. If false, explain why.
- The impressive functions performed by computers essentially involve the manipulation of zeros and ones.
- People prefer to manipulate bits instead of characters and fields because bits are more compact.
- People specify programs and data items as characters; computers then manipulate and process these characters as groups of zeros and ones.
- A person's 5-digit zip code is an example of a numeric field.
- A person's street address is generally considered to be an alphabetic field in computer applications.
- Data items represented in computers form a data hierarchy in which data items become larger and more complex as we progress from fields to characters to bits, etc.
- A record key identifies a record as belonging to a particular field.
- Most organizations store all information in a single file to facilitate computer processing.
- When a program creates a file, the file is automatically retained by the computer for future reference; i.e., files are said to be persistent.
|
17.7 |
Exercise 17.3 asked the reader 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 is 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 are 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 17.3) is applied to the master file (called oldmast.dat in Exercise 17.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 17.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).
|
17.8 |
After writing the program of Exercise 17.7, write a simple program to create some test data for checking out the program. Use the following sample account 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
|
Transaction file Account number
|
Transaction amount
|
100
|
27.14
|
300
|
62.11
|
400
|
100.56
|
900
|
82.17
|
|
17.9 |
Run the program of Exercise 17.7, using the files of test data created in Exercise 17.8. Print the new master file. Check that the accounts have been updated correctly.
|
17.10 |
It is possible (actually common) to have several transaction records with the same record key. This occurs because a particular customer might make several purchases and cash payments during a business period. Rewrite your accounts-receivable file-matching program of Exercise 17.7 to provide for the possibility of handling several transaction records with the same record key. Modify the test data of Exercise 17.8 to include the following additional transaction records:
Account number
|
Dollar amount
|
300
|
83.89
|
700
|
80.78
|
700
|
1.53
|
|
17.11 |
Write a series of statements that accomplish each of the following. Assume that we have defined class Person that contains private data members
char lastName[ 15 ];
char firstName[ 15 ];
char age[ 4 ];
and public member functions
// accessor functions for lastName
void setLastName( string );
string getLastName() const;
// accessor functions for firstName
void setFirstName( string );
string getFirstName() const;
// accessor functions for age
void setAge( string );
string getAge() const;
Also assume that any random-access files have been opened properly.
- Initialize the file nameage.dat with 100 records that store values lastName = "unassigned", firstName = "" and age = "0".
- Input 10 last names, first names and ages, and write them to the file.
- Update a record that already contains information. If the record does not contain information, inform the user "No info".
- Delete a record that contains information by reinitializing that particular record.
|
17.12 |
You are the owner of 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 following information to start your file:
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
|
|
17.13 |
(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:
Digit
|
Letter
|
2
|
A B C
|
3
|
D E F
|
4
|
G H I
|
5
|
J K L
|
6
|
M N O
|
7
|
P R S
|
8
|
T U V
|
9
|
W X Y
|
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 is 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." The owner of a liquor store would, no doubt, be delighted to find that the store's telephone number, 233-7226, corresponds to "BEERCAN." A veterinarian with the phone number 738-2273 would be pleased to know that the number corresponds to the letters "PETCARE."
Write a C++ 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.
|
17.14 |
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 datasize.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 right column, as in:
char 1
unsigned char 1
short int 2
unsigned short int 2
int 4
unsigned int 4
long int 4
unsigned long int 4
float 4
double 8
long double 10
|
[Note: The sizes of the built-in data types on your computer might differ from those listed above.]
|
|