-3

I have looked around Stack Overflow and did some work myself with this code but doesn't matter whatever I do the answer prints to false but it has to print to true, also how can I Plot their running times as a function of their input size as scatter plots and then choose representative values of the size n, and run at least 5 tests for each size value n in the tests?

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 double[] prefixAverages(double[] inArray) {
    double[] outArray = new double[inArray.length];
    return outArray;

}


public static void main(String... args) {
System.out.println(
   Arrays.equals(
      prefixAverages(new double[] {5, 6, 7, 8}),
      new double[] {2, 2.5, 3.5, 4}
   )
);
}
}

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 double[] prefixAverages(double[] inArray) {
double[] outArray = new double[inArray.length];
return outArray;

}


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

}

3 Answers3

0

I'm guessing because it's hard to understand what you are trying to do. But right now I'm not even sure how this code compiles, because you haven't defined any constructors, and you are (apparently attempting to) call a constructor (with the same name of a static class function, confusing AND bad form) with an array parameter. I'm guessing you meant to call the static method that takes a double[] parameter. (Right now, and here's the confusing part, it shouldn't compile because you should have to prefix the class name to call a static function.). Even that would fail, though, because that method returns an empty array of the same length of the input array. To top it all off, the "algorithm" section of your code is never even called.

I'd suggest just stepping through this in a debugger. That way you could see what the code is doing in real time and correct it if it's not what you meant.

Also, if you'd followed normal coding conventions (never name a function after the class; only constructors), it would be easier to spot errors like that.

Edit: I see how it compiles now. Still, naming the function after the class makes me think "constructor", along with everyone else on the planet.

Ron Thompson
  • 1,086
  • 6
  • 12
0

First you must understand what Arrays.equals() method does. The Arrays.equals() method does comparison operation on two arrays, based on their content; not based on their size. First of all, let's look at your prefixAverages(float [] inArray) method. What you did there is just create a new float array of size equals to the size of the passing array, and then return that newly created array. And in the main method, you are comparing the returning array and a new array that you are supplying. But this is logically not correct. Since the returning array is an empty array; the Arrays.equals() method returns false, as the other array has some values in it. Even if the returning array do have some values, the Arrays.equals() method returns false if both contain different values. Always remember two arrays are equal if and only if they contain same values and the elements should be in the same order. ** Modify your program like this and it'll return **true

import java.util.Arrays;

public class PrefixAverages1 {

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


public static void prefixAverages(){

for (int i = 0; i < array.length; i++){

    double s = array[i];

        for (int j = 0; j < array.length; j++){

            s = s + array[j];
        }

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

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

}
public static double[] prefixAverages(double[] inArray) {   
    double [] outArray = inArray;
    return outArray;

}


public static void main(String... args) {
System.out.println(
   Arrays.equals(
      prefixAverages(new double[] {5, 6, 7, 8}),
      new double[] {5, 6, 7,8}      
   )
);
}
}

One more thing. While using arrays in loop; always use array.length method to do comparison condition checking. Otherwise there is a high change of probability for an ArrayOutOfBoundsException condition.

JHermit
  • 190
  • 9
0

The output you expect is incorrect; for the input 5, 6, 7, 8 the prefix average should be 5.0, 5.5, 6.0, 6.5.

After the first value 5, there has been one value (5). After the second value (6 + 5) there have been two values 11 (and 11 / 2 is 5.5). Then 6 (because 6+5+7 is 18) and 18/3 is 6. Finally 18+8 is 26 and 26/4 is 6.5

static double[] prefixAverages(double[] x) {
    int len = x.length;
    double[] arr = new double[len];
    double sum = 0;    
    for (int i = 0; i < len; i++) {
        sum += x[i];
        arr[i] = sum / (i + 1);
    }
    System.out.println(Arrays.toString(arr));
    return arr;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249