Tweet |
Custom Search
|
|
In this example, the DATA SAVE statements are used to open a file and write
a file name on it (line 10), write data to the output file (line 40), and
write an end-of-file indicator on the file (line
60). Since the file processing statements
will probably be different on the system you are using, you will have
to refer to the users reference manual to determine what statements perform
these functions. Once the master file has been created you might want to print it, either to ensure that all items have been entered and entered correctly; or you may wish to have a listing on which you can keep a tally of items sold and use it when you update the master file. The following program could be used to print the master file.Example: In this program, the DATA LOAD statement, line 20, opens the master file (INVFILE) and makes it available for processing. The PRINT statements in lines 30 and 40 print the column headings. Lines 50 through 80 comprise the loop which will control the reading and printing of the master file. The DATA LOAD statement in line 50 reads the values for the variables and makes them available for the PRINT statement to print. The IF END statement in line 60 checks for end-of-file. At end-of-file, control is transferred to line 99 where the END statement terminates the program.When reading a file and the end-of-file indicator is read, the computer will detect it. If any future attempt is made to read from the file before it is reopened, an end-of-file error message is printed and the computer stops executing the program. When reading a file, the BASIC statement IF END can be used to test whether the computer did, in fact, read the end-of-file indicator. It also transfers control to any specified line number in a program. For example, you could transfer control to a PRINT statement to print summary totals, or transfer control to an END statement to terminate the program as was done in this example. Depending on what computer you are using, the IF END statement is normally placed either immediately following or immediately preceding a read statement.Now that we have created and printed the master file, we can now begin to use it. For example, well need to update the file so the on-hand quantity for each item will be current and well know when a particular item needs to be reordered. The flowchart (figure 7-2, a foldout at the end of this chapter) is a pictorial representation of the procedures required to solve the problem. The problem solution has been divided into four parts:
While some of these parts could have been combined, they were kept separate for ease of understanding.Study the flowchart and examine its four distinct parts. Part one consists of blocks 1 through 4 which are used to read the parts data into arrays and to accumulate a total count of the records in the file. This total will be used to control successive FOR-NEXT loops.Part two consists of blocks 5 through 12 which are the update portion. This section allows you to select part numbers at random to be updated. Using the part number entered in block 5, the FOR-NEXT loop compares it to each part number in array P. When a match is found, the subscript X equals the parts relative position in the arrays and control is transferred to block 11. Block 11 allows you to enter quantity sold. Block 12 uses the value of X as the subscript to select the corresponding on-hand quantity, Q(X), to be updated. Q(X) is used with the quantity sold, Q1, to compute the new on-hand quantity. Then control is transferred back to block 5 for another part number to be entered. If a match is not found, this means the entire array has been searched in the FOR-NEXT loop, blocks 7 through 9, and the part number (P 1) entered in block 5 did not match any part number in the parts number array, P(X). Therefore, an error message is printed and control is transferred back to block 5 to enter the corrected part number or another part number. If no more updating is required, 9999 is entered and block 6 tests this condition. If it proves true, control is transferred to block 13 which is the first block of part three.Part three consists of blocks 13 through 17 which generate a list of parts below stock level. Blocks 14 through 17 comprise a FOR-NEXT loop. Block 15 tests if the on-hand quantity is greater than the reorder-point quantity. If this proves false, the part number, part name, and unit price of that part is printed on the below stock level list. Block 17 tests for X equal to K. If it is not equal, control is transferred to block 14 which increments the value of X by one and continues searching the records for items to be reordered. Once X is equal to K (the number of records in the file), control is transferred to block 18, which is the first block of part four.Part four consists of blocks 18 through 22 which create the new master file. Blocks 19 through 21 comprise a FOR-NEXT loop which is used to control the writing of records to the new master file. Block 20 writes the new records onto an auxiliary storage medium; block 21 tests for X equal K. If X-is not equal to K, control is transferred back to block 19 which increments X by 1 and block 20 writes another master record. Once X is equal to K, control is transferred to block 22 which closes the new master file and the program is terminated, block 23.Examine the program (figure 7-2) and see how the program coding corresponds to the blocks in the flowchart.In this program, lines 50 through 130 are used to read the master file into the computers memory. Lines 150 through 300 are used to update the master file. In line 190 the part number to be updated is entered; or if no more parts are to be updated, 9999 is entered. Line 200 tests P1 for end of update data (9999); if not, control goes to line 210 which is the beginning of a FOR-NEXT loop that searches array P for a matching part number. If a matching part number is not found, control is then transferred to line 240 which prints the message, PART # NOT FOUND. If a matching part number is found, control is transferred to line 260 which displays the data elements of the record to be updated. Line 270 requests quantity sold to be entered. Line 290 computes new on-hand quantity by subtracting Q1 from Q(X). Then the GOTO statement in line 300 returns control to line 180 which is a prompt to enter another part number to be updated.When no more parts records are to be updated, control is transferred to line 340 which prints the heading for the below stock level list. Lines 350 through 380 comprise a FOR-NEXT loop which is used to search the on-hand quantity and the reorder point quantity arrays. Line 360 tests the updated on-hand quantity, Q(X), and the reorder point quantity, R(X), to determine if Q(X) is greater than R(X). If it is, the data for another record is tested. If it is less than or equal to R(X), the part number, part name, and unit price are printed on the list. Once all parts have been tested for reorder criteria, lines 400 through 470 write the updated master file to an auxiliary storage medium.In this program we chose to read all the data into arrays to allow updating the parts records in any sequence, and to allow a parts record to be updated more than once in a single run. This will work for small data files. However, for larger data files (those that cannot fit into memory) other processing methods would be needed.SUMMARY As you write programs to be used repetitively and to solve more complex problems, the need for storing and retrieving programs becomes more apparent. Although the specific statements for storing and retrieving programs will vary on different computers, the concepts will be similar. Programs must be assigned a name so they can be referenced and reloaded. A program name is a series of characters used to identify a program. A system command, such as SAVE, is used to store a program. To reload a program will require another system command in which you specify the name of the program to be loaded. Once a program is loaded into a computers memory, it is available to be executed, changed, or listed.The need for a method to store and retrieve data files becomes apparent when the same files are to be used extensively. When data is stored in a program, you are restricted in what can be done with it. This data cannot be used with other programs and it would be very difficult, if not impossible, to update. To eliminate these restrictions, data can be stored on auxiliary storage media such as magnetic disk or tape. Data stored in this manner is called a data file. A file consists of a group of related records and each record consists of fields. A field is a specified area in a record used for a particular category of data.Most computers require a file processing statement to make a file ready to be read or written. This is usually some type of open statement and includes the file name and the tape or disk where the data will be written or read. Once the file is opened, file processing statements are used to read data from a storage medium into memory or to write data from memory to a storage medium. Once you are through processing a data file, you must tell the computer you are finished with that file. This is done with some type of close statement.Storing data independently from programs allows more flexibility. Data stored in this manner can be used by different programs. It can be updated, printed, or used to generate various reports.The programming concepts presented in this text are fundamental; they are intended to get you started in programming. As your knowledge and experience increase, you will discover new and more sophisticated ways to use the language to solve more difficult problems. |
||