Wben it comes to representing sequences of values, there are typically two data structures used:
As in past weeks, we will solve the same (simple) problem in each of our four languages, to compare their array constructs. This week's problem is this: write a function that, given an array of values, computes and returns the average of the values in the array. Solving this problem will give us the opportunity to study each language's array construct, including
As before, we will provide program "skeletons" that provide a framework for calling the function that solves our problem, using an implementation of the following simple algorithm:
1. Define theArray as an array containing 9.0, 8.0, 7.0, 6.0.
2. Call Average() to compute the average of the values in theArray,
and display the value it returns.
To perform step 2 of this algorithm, our "program" will use a subprogram
to compute and return the average of the values in an array.
The specification of this subprogram is something like:
Receive: anArray, an array of numbers,
itsSize, an integer equal to the size of anArray.
Precondition: itsSize >= 0.
Return: the average of the values in anArray.
Note that parameter itsSize may be unnecessary in
languages whose arrays are "smart"
(i.e., languages whose arrays "know" their size).
To solve this problem, we will use the following algorithm:
If the number of values in anArray <= 0,
Return 0.0.
Otherwise,
Return the sum of the values in anArray / the number of values in anArray.
To test our work, we will use two arrays:
As usual, we will lead you through the process using Java, after which you will apply similar techiques to solve the problem in the other three languages. As usual, the order in which you do the three exercises does not matter.
Begin by making a new directory for this lab exercise and changing directory to that new directory.
Turn in.
Each of the four parts requires you to create files
containing source code and execution traces.
When you have completed them, use
cat to create a single file that contains all of your results:
cat script.java script.ada script.clojure script.ruby > lab07-resultsThen submit your work by copying that single file into your personal folder in /home/cs/214/current/:
cp lab07-results /home/cs/214/current/yourUserName
replacing yourUserName with your login name.
The grader will access and grade your results from there.