The University of Texas as San Antonio Division of Mathematics, Computer Science, and Statistics San Antonio, Texas 78285 Programming Assignment 1: Copy ProgramÐ Finding Average and Sample Variance CS 2073, Engineering Programming Spring Semester, 1992 The objectives of this assignment are to introduce you to: · using the IBM PC, in the PC lab, · using the Turbo Pascal editor to enter two files, one a Pascal program and one a data file, and · using the Turbo Pascal system to compile and execute the Pascal program. (During execution this program reads the data file.) Your text has an appendix (Appendix J on pages A42±A71) that describes the use of Turbo Pascal on PC's. What follows below are very brief directions just to get you started. (In what follows, RET means the ªcarriage returnº or ªenter key,º and DEL means the ªdeleteº key. Use the special arrow keys, not the ones on the keypad.) 1. Follow the directions for the PC Lab: type ªCS2073_01 A:º (or B:) in response to the ªEnter your login name:º request. Use the ¯ key to go through the ªApplications Menuº to ªTurbo Pascal 5.5º. Type RET. Answer which drive contains your data disk. (Type ªA:º for the upper one and ªB:º for the lower.) 2. Type RET. Use arrow keys to get to ªFileº at the top. (You may already be there.) 3. Use ¯ key to get to ªNewº. Type RET. 4. Type in the Pascal source program given at the end of this handout, using arrow keys to get around, using DEL to get rid of unwanted items, and using RET to get a new line. (DEL removes the character before the cursor.) 5. Type F10 to get back to the top menu. 6. Again arrow to ªFileº, use ¯ key to ªSaveº, and type RET. (The first time you will need to type in a file name, like ªA:\AVE.PASº. (This assumes you are using the top drive.) The next ªSaveº will assume this same name. If you want to save using a different name, use ªWriteToº.) 7. Now arrow to ªFileº and ªNewº as before and key in a short list of scores, say one that looks like: 80.0 100.0 90.0 (Be careful to have no RET after the final 90.0.) 8. Save this new file using the name ªA:\SCORES.DATº, as in 5 and 6. 9. Arrow to ªRunº, ¯ key to ªRunº below, and type RET. 10. If there are any compiler errors or run-time errors, you will pop into the proper place in the editor to change these errors. Start in again at step 8. 11. In case the program runs correctly, you can see the output on the screen either by quitting Turbo Pascal (see 12 below) or by typing ALT/F5. (Hold down the ALT key and type F5. This will toggle back and forth from the screen to Turbo.) The output should look like: The scores are: 80.0 100.0 90.0 Number of scores: 3 Average of scores: 90.00 Sample variance of scores: 10.0 12. Quit the Turbo environment by arrowing to ªFileº and then ¯ key to ªQuitº and then RET. 13. If you later want to change this program, arrow to ªFileº, ¯ key to ªLoadº, and type ªA:\AVE.PASº, followed by RET. 14. Finally to get a listing of your program to turn in, first type ESC, then arrow to ªSystem Servicesº, then RET, then arrow to ªRun a DOS Commandº, and finally type ªPRINT A:\AVE.PASº at the main DOS prompt. Type a final RET. There are a number of ªhotº keys that give shortcuts to allow you to work quicker. You can get to information about these keys using the ªhelpº key F1. (ESC gets you out of help.) There are also shortcut keys within the Turbo editor, including Home to get to the beginning of a line, Een to go to the end of a line, PgUp to go one page up, and PgDn to go one page down. program average_and_dev; (*------------------------------------------------------------------ * Name: insert your name here * Date: insert your due date here * Course: CS 2073, Section 001 *------------------------------------------------------------------ * Program: Compute the average and sample variance of a file of * scores. * Algorithm: Scores are read, summed, and counted. Then the average * is computed. A second reading of the file allows the * sample variance to be calculated. *------------------------------------------------------------------ * Input: Input scores are real numbers, one to a line, in the * file named SCORES.DAT. * Output: Output is the average and the sample variance. *------------------------------------------------------------------ *) var average: real; (* average of scores *) samplevar: real; (* sample variance of scores *) count: integer; (* the number of scores read in *) score: real; (* the current score being read and processed *) sum: real; (* running sum of scores *) sumsq: real; (* the sum of squares of scores - average *) infile: text; (* internal name for the input file ´) begin (* average_and_dev *) assign(infile, 'SCORES.DAT'); reset(infile); sum := 0.0; count := 0; writeln('The scores are:'); while not eof(infile) do begin readln(infile, score); writeln(score:7:2); sum := sum + score; count := count + 1 end; close(infile); (* if count <> 0 or 1, calculate average and sample variance *) if count = 0 then writeln('*** No scores, and empty file ***') else if count = 1 then writeln('*** Only one score ***') else begin average := sum/count; sumsq := 0; reset(infile); while not eof(infile) do begin readln(infile, score); sumsq := sumsq + sqr(score - average) end; close(infile); samplevar := sqrt(sumsq/(count - 1)); writeln('Number of scores: ', count:1); writeln('Average of scores: ', average:3:2); writeln('Sample variance of scores: ', samplevar:3:2) end end.