CSC 153  Grinnell College  Spring, 2005 
Computer Science Fundamentals  
Laboratory Exercise  
This laboratory exercise considers output procedures to aid in the tracing of a program. This lab also discusses interactive Scheme programming, using read, display and write statements. Finally, the lab mentions the use of sentinel values to halt the reading of data from the keyboard.
Consider the following two versions of a procedure to add 2 to each element in a list:
(define simpleadd2 (lambda (L) ;Precondition: L is a list of numbers ;Postcondition: returns a new list where 2 is added to each value in L (if (null? L) '() (cons (+ 2 (car L)) (simpleadd2 (cdr L)))) ) ) (define simpleadd2trace (lambda (L) ;Precondition: L is a list of numbers ;Postcondition: returns a new list where 2 is added to each value in L (display L) (newline) ;;NEW: print out current list on separate line (if (null? L) '() (cons (+ 2 (car L)) (simpleadd2trace (cdr L)))) ) )
Check that procedure simpleadd2 works correctly, by running it
with the command
Run the revised proceduresimpleadd2trace with the same data you
used in styp 1, by typing
(simpleadd2trace '(2 6 3 0 4.689))
Write a few sentences to explain what appears at your screen:
Experiment further with display and newline.
Now consider this furtherrevised version of simpleadd2trace:
(define simpleadd2trace (lambda (L) ;Precondition: L is a list of numbers ;Postcondition: returns a new list where 2 is added to each value in L (display L) (if (null? L) (begin (display " then clause") (newline) '() ) (begin (display " else clause") (newline) (cons (+ 2 (car L)) (simpleadd2trace (cdr L))) ) ) ) )
(define simpleadd2trace (lambda (L) ;Precondition: L is a list of numbers ;Postcondition: returns a new list where 2 is added to each value in L (display L) (if (null? L) (begin '() (display " then clause") (newline) ) (begin (cons (+ 2 (car L)) (simpleadd2trace (cdr L))) (display " else clause") (newline) ) ) ) )
Consider the following procedure that reads 3 scores from the keyboard and averages them:
(define average3 (lambda () ;Precondition: None ;Postcondition: returns the average of 3 numbers read from the keyboard (display "Enter three numbers: ") (display (/ (+ (read) (read) (read)) 3.0)) (display " is the average of the three numbers") (newline) ) )
What happens if you enter three fractions or some negative numbers when you run this procedure?
What happens if you enter a string instead of a number when you run this procedure?
Suppose you wanted to average any number of values; you might ask the computer to continue reading numbers until you entered a 0. In this context, you could pass the current sum and the number of values read to the kernel procedure. The base case would be identified by checking if the current value read was zero.
Write an average procedure which reads successive numbers until 0 is entered and computes and prints the average of these numbers. Be sure you do not include 0 in your computation of the average.
Note: In this problem, the number 0 is called a sentinel. Generally, a sentinel is a value read that tells the computer something special about how processing should proceed.
Consider the following procedures to create a table of square roots:
(define sqrttable (lambda (n) (display "Number Square Root") (newline) (sqrttablehelper n) ) ) (define sqrttablehelper (lambda (n) (if (>= n 1) (begin (sqrttablehelper ( n 1)) (display n) (display (sqrt n)) (newline)) ) ) )
Run this procedure to obtain a table of the square roots of the integers 1, 2, 3, 4, and 5.
Within your Scheme environment, load the customized I/O package ~walker/151s/labs/writeformatted.ss with the statement:
(load "~walker/151s/labs/writeformatted.ss")
This package, written by John David Stone, contains procedures writeint and writereal for formatting integer and real numbers, respectively. The writeint procedure asks for the number to be printed and the width of the field for printing. In our example, we want 4 spaces for our integer n (3 blank spaces plus the digit). Thus, we write
(writeint n 4)Similarly, for real numbers, we supply writereal with the number to be printed, the total width for printing the number, and the desired number of decimal places. In this example, the number takes 14 characters overall, with 5 decimal place accuracy:
(writereal (sqrt n) 14 5)
Replace the display
statements in
sqrttablehelper
by the corresponding writeint
and writereal statements, and check that the new formatting works
as required.
Run writeint
with various integers and widths to check that
it runs correctly. What happens if writeint
is given a width
that is narrower than required by the number to be printed (e.g., 2 spaces
allocated for the printing of 456)?
Run writereal with a several real numbers, widths, and fractionallengths to check that it also runs correctly.
This document is available on the World Wide Web as
http://www.walker.cs.grinnell.edu/courses/153.sp05/labs/labio.shtml
created March 5, 1997 last revised February 2, 2005 

For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. 