CSC 012

Introduction to Computer Science

 

Summary of 4/3 Lecture

We began by proposing an alternative solution to the problem of averaging a list of scores, discussed on 3/18.  That solution combined the input of scores and summing of scores in one loop as follows:

        while (true)
        {
            //Get input from user
            in.setPrompt("Enter score: (OK to quit)");
            score = in.readInt();
            if (in.eoi())
            {
                break; // out of the loop
            }
           
            count = count + 1; //increment the counter
            sum = sum + score; //accumulate the sum of the scores entered
        }

However, there are many circumstances in which we might want to average a list of integers, independent of the process of input.  To that end, we propose the following strategy:

  1. Input the scores.  This will clearly involve a loop.

  2. Average the scores.  This too will require a loop.

  3. List the scores and the average.  The process of listing the scores will obviously require a separate loop.

We modified the above code to accommodate the input of scores as follows:

//Input the scores
in.setPrompt ("How many scores?");
int n = in.readInt();

for (int k=0; k<n; k++)
{
        in.setPrompt ("Enter score");
        int score = in.readInt();
}

//Average the scores
At this point, we discover that the computer will remember only the LAST SCORE ENTERED!   Since the variable score is a simple primitive data type, it is capable of holding only one score at a time.  We clearly need another, more complex data structure in order to accomplish our task.

Arrays

An array is a euphism for subscripted variable.  It allows us to acquire and access a block of memory locations, using only one identifier.  The following implementation of the problem described above illustrates.

//File: Averaging.java
import CSLib.*;
public class Averaging
{
    public static void main (String [] args)
    {
        //Input the scores
        InputBox in = new InputBox ();
   
        in.setPrompt ("How many scores?");
        int n = in.readInt ();
   
        int [] score = new int [n]; //reserves n contiguous memory locations
                                        //The index or subscript starts at 0, not 1
   
        for (int k=0; k<n; k++)
        {
            in.setPrompt ("Next score");
            score [k] = in.readInt ();
        }
   
        //Average the scores
        int sum = 0;
        for (int k=0; k<n; k++)
        {
            sum = sum + score [k];
        }
        int avg = sum / n;
   
        //Display the scores
        OutputBox out = new OutputBox ();
        out.println ("The list of scores follows:");
        for (int k=0; k<n; k++)
        {
            out.println (score [k]);
        }
        out.println (); //double space
   
        //Display the average
        out.println ("The average of the scores is: " + avg);
    }
}

We observed that the process of averaging is one that might occur frequently in our programs.  So, it's worth encapsulating in the form of a function.   We decided to gather together such useful procedures into a separate class as follows:

public class NumberList
{
    public static int average (int [] list, int number)
    {
        int sum = 0;
        for (int k=0; k<number; k++)
        {
            sum = sum + list [k];
        }
        return sum / number;
    }
}

Note that the presence of the keyword static makes the method "average" a "class" method.  That is, it goes with the class.  So the application program, Averaging.java, is modified as follows:

        ...
        //Average the scores
        int avg = NumberList.average (score, n);
...
   
Note that the actual parameters, score and n, used in Averaging.java must match in number and in type the formal parameters, list and number, employed in the NumberList class.

Finally, we added a display() method to NumberList which made the Averaging class even more succint.

        //Average the scores
        int avg = NumberList.average (score, n);
   
        //Display the scores
        OutputBox out = new OutputBox ();
        out.println ("The list of scores follows:");
        NumberList.display (out, score, n);
           
        //Display the average
        out.println ("The average of the scores is: " + avg);


Lab Exercise  Add a method that calculates the standard deviation of a list of doubles to the NumberList class.  The signature for the method will look like:

static int sd (int [] list, int avg, int n)


Back to CSC 012 Home Page