0
votes

I am attempting to translate two separate algorithms into Java code in order to test them; the first algorithm (PrefixAverages1) is:

For i = 0 to n-1
    Let s = X[0]
    For j = 1 to i
        Let s = s + X[j]
    End For
    Let A[i] = s / (i + 1)
End For

Output: An n-element array A of numbers such that A[i] is the average of elements X[0], X[1], ..., X[i].

This is the Java translation that I came up with for PrefixAverages1:

import java.util.Arrays;

public class PrefixAverages1 {

static double array[] = new double[10];

public static void prefixAverages(){

    for (int i = 0; i < 10; i++){

        double s = array[i];

            for (int j = 0; j < 10; j++){

                s = s + array[j];
            }

        array[i] = s / (i + 1);

        System.out.println(Arrays.toString(array));
    }

}

public static void main(String[] args){
    prefixAverages();
}

}

The second algorithm (PrefixAverages2) is:

Let s = X[0]
For i = 0 to n-1
    Let s = s + X[i]
    Let A[i] = s / (i + 1)
End For

Output: An n-element array A of numbers such that A[i] is the average of elements X[0], X[1], ..., X[i].

This is the Java translation that I came up with for PrefixAverages2:

import java.util.Arrays;

public class PrefixAverages2 {

static double array[] = new double[10];

public static void prefixAverages(){

        double s = 0;

        for (int i = 0; i < 10; i++){
                s = s + array[i];
                array[i] = s / (i + 1);
        }
            array[0] = 10;

        System.out.println(Arrays.toString(array));
}

public static void main(String[] args){
    prefixAverages();
}

}

I am attempting to test the algorithms but am not quite sure where to start. I am attempting to set up an array and then apply these algorithms on it by running through them by hand for small values of n.

Where/how would I begin to add elements to the array to begin with?

I am also trying to analyse both algorithms by counting primitive operations and derive T(n) for both algorithms. I would like to be able to work out the time complexity (Big Oh, O(n)) of each algorithm and discover which one is the most efficient.

Any help with this would be greatly appreciated.

Thanks for taking the time to read and assist.

1
Is there a good reason that you're using one array for input and output? You should separate the two, at least for now. Your first implementation certainly won't work because you overwrite parts of the array that you need to read later. - Mark Peters
@Mark Thank you for your reply, Mark. I am merely following what the algorithm pseudocode said, but maybe I have done something wrong as you said. Would you be able to kindly suggest an alternative way of doing this? Thank you. Mick. - Mus
take a look at your pseudocode. It uses a different array for output and input (A[] vs X[]). - Mark Peters

1 Answers

3
votes

First of all, you should pass in the array to the function and return the result.

public static double[] prefixAverages(double[] inArray) {
    double[] outArray = new double[inArray.length];
    //...compute the averages...
    return outArray;
}

Then you can do some manual testing in you main method:

public static void main(String... args) {
    System.out.println(
       Arrays.equals(
          prefixAverages(new double[] {1, 2, 3, 4}),
          new double[] {1, 1.5, 2, 2.5}
       )
    );
}

Make sure this code prints true.

Now, that's not a very robust solution because of accuracy limitations with floating-point numbers, but it should get you started. If you want to really look into testing your code, you should research using a test harness like JUnit.