Laboratory Exercises For Computer Science 153

File Examples

File Examples

Goals: This laboratory exercise provides more practice with files.

  1. Write and test a Scheme procedure that takes two arguments -- the name of an input file containing zero or more integers, and the name of an output file to be created by the procedure -- and copies each integer from the input file to the output file if it is in the range from 0 to 99. Values outside of this range should be read in but not copied out again. The idea is that this procedure will act as a filter, ensuring that only the values that are in the correct range will make it into the output file.

    Line breaks in the input file should be ignored. In the output file, arrange for each integer to be printed on a line by itself.

  2. Here is a Scheme procedure that copies an input file to an output file character by character:
    (define copy-file
      (lambda (source-file-name target-file-name)
        (let ((source (open-input-file source-file-name))
              (target (open-output-file target-file-name)))
          (let loop ((ch (read-char source)))
            (if (eof-object? ch)
                (begin
                  (close-input-port source)
                  (close-output-port target))
                (begin
                  (write-char ch target)
                  (loop (read-char source))))))))
    
    Modify this procedure so that every lower-case letter that is read in is converted to upper case before being written to the output file.

  3. Write a Scheme procedure tally-char that takes two arguments, the name of an input file and a character, and returns a tally of the number of occurrences of that character in the specified file.
    (tally-char "/home/walker/153/labs/data.1" #\5) ===> 4
    (tally-char "/home/walker/153/labs/data.2" #\0) ===> 7
    (tally-char "/home/walker/153/labs/data.2" #\newline) ===> 2
    
  4. File "/home/walker/153/labs/data.3" contains laboratory data organized by lines, with two numbers on each line, as follows;
    102.7   81.2
     76.18  91.8
    -36.4   76.4
     18.53  80.3
    
    Write a procedure process-file that has a file name as argument and, for that file, finds the minimum of the first numbers in each line (102.7, 76.18, -36.4, 18.53 in the example) and the average of the second numbers (81.2, 91.8, 76.4, 80.3 in the example). Thus, the procedure call
    (process-file "/home/walker/153/labs/data.3")
    
    should return (-36.4 82.425).

    In developing your procedure for a general file, you may assume that the file is not empty.

  5. One can assess how "randon" a random number generator is by applying various statistical tests. Some simple tests are illustrated in this problem.

    1. Use Scheme's random procedure (from the lab in simple simulations) to write a procedure generate-data, which takes a file name and number n as arguments and which writes n random integers between 1 and 10 (inclusive) to that file. On the file, each number should be printed on a separate line .

    2. Write a procedure that takes a file name as argument, and that computes the following:
      • the average of the numbers in the file,
      • the number of 0's, the number of 1's, ..., the number of 10's that appear in the file,
      • the average distance from one number to the next in the file.
      (If the file contained random integers between 0 and 10, then one might expect the average to be about 5.0, each number to appear about the same number of times, and the average distance between numbers to moderately large.)


This document is available on the World Wide Web as

http://www.math.grin.edu/~walker/courses/153/lab-file-examples.html

created March 11, 1997 by John D. Stone
last revised January 30, 1998 by Henry M. Walker