For example, if the input were
5 3.7 5.2 -4.7 -62 14.7
the output might be
Sum: -43.1
Average: -8.62
3 of your 5 numbers are positive.
Also turn in sample runs with well-chosen test cases.
Note: in C or C++, you can demonstrate the use of pointers and memory management by using a pointer rather than an array declaration, and allocating exactly as much memory as necessary (in which case you no longer have the restriction of 100 numbers). In Java, this is the only natural way to do it.
In Java, you could try doing the same thing all over again using an ArrayList or LinkedList instead of an array. (A competent Java programmer should know how to do both.)
Yet another way to represent the data would be to write your own implementation of linked lists (in C, C++, or Java).
Write three functions sum
,
average
, and count-positive
, each of which
takes in a list of numbers and returns a number.
If you want
to practice your Scheme I/O, you can then write a 0-parameter function
named hw2
that asks the user for a number, prints an error
message if it's less than 0, more than 100, or not an integer,
and otherwise asks for that many numbers, forms them into a list, and
prints the results as above. (It can, of course, call the
previously-written list functions to do most of the work.)
If you want to learn about Scheme vectors, try doing the same thing using a vector (sorta like an array) rather than a list.
As in the Scheme version, using Prolog lists.