(This is a good exercise to try in each language.)
Write a program
that reads in three double numbers,
prints their sum and average (appropriately labelled), and prints out how
many of them are positive. For example, if the input were
3.7 5.2 -4.7
the output might be
Sum: 4.2
Average: 1.4
2 of your 3 numbers are positive.
Also turn in sample runs with well-chosen test cases.
Write three functions named sum
,
average
, and count-positive
. Each will take
in three numbers and return a number: the sum of the three numbers,
their average, or how many are positive, respectively.
If you want to practice with Scheme I/O, write a
function named hw1
that takes no arguments, asks the user
for three numbers, then prints out their sum, average, and how many are
positive, as above. Hint: use the functions you
just wrote!
Write three 4-place predicates named
sum
, average
, and count_positive
.
Each one expects its first three arguments to be bound to numbers, and
binds the fourth argument to a number as appropriate.
If you want to practice with Prolog I/O, write a
0-place predicate named hw1
that asks the user for three
numbers, then prints out their sum, average, and how many are positive,
as above. Hint: use the predicates you just wrote!