-4

So I've been tasked with completing this program for my AP Computer Science class:

Write the code that takes an array of doubles and moves all the values greater than the average to fill the array to the left. Any values less than the average that haven't been written over should be set to 0. First calculate the average and store it in a variable named average.

Example: The array [1.0 , 1.0, 3.0 4.0 ], the average is 2.25 would be transformed to [3.0, 4.0, 0.0, 0.0]

public class Array {

  public static void main(String[] args) {

    double[] arr = {
      1.0, 2.0, 3.0, 4.0, 5.0
    };
    double average;
    double sum = 0.0;

    for (int i = 0; i < arr.length; i++)
      sum += arr[i];
    average = sum / arr.length;


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

      if (arr[i] >= average)
        arr[i] = arr[i];
      else if (arr[i] < average) {
        arr[i] = 0.0;
        arr[i] = arr[i];
        for (int t = i + 1; t < arr.length; t++) {
          if (arr[i] >= average)
            arr[i] = arr[i];
        }
      }


    }
    for (int y = 0; y < arr.length; y++)
      System.out.println(arr[y]);
  }
}

and the output is:

0.0
0.0
3.0
4.0
5.0

I've been working on this for a few hours and still can't get the desired output which should be : 3.0 4.0 5.0 0.0 0.0

Anyone know what I'm doing wrong?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Leslie A.
  • 3
  • 1
  • 2
  • What do you believe `arr[i] = arr[i]` does, and why do you believe it does anything at all? If you have to move a value from one place to another, don't you think the indexes need to be .... you know .... *different*? --- *Hint:* You need two index variables. – Andreas Dec 17 '16 at 03:31
  • `arr[i] = arr[i];`? – Danh Dec 17 '16 at 03:31
  • Do it in two step; set to zero; shift the values. – dreamcrash Dec 17 '16 at 03:35

2 Answers2

0

For the second step make two indexes: current index i and index of the place to fill j

At every i-step: if value at current index is larger than average, copy it into j-th element and increment j

I hope that the last step with zero-filling after the last j is simple enough

MBo
  • 77,366
  • 5
  • 53
  • 86
-1
public class RearrangeArray {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
         double[] arr = {1.0,2.0,3.0,4.0,5.0};
         double[] newArr = new double[arr.length];

         double sum = 0.0;  
         for (int i = 0; i < arr.length; i++)
              sum += arr[i];
         double average = sum / arr.length;

         System.out.println("average = " + average);
         int n = 0;
         for(int i = 0; i < arr.length; i++)
         {
            boolean check = compare(arr[i],average);
            if(check)
            {
                newArr[n] = arr[i];
                n++;
            }
         }

         for (int i = 0; i < arr.length; i++)
         {
            System.out.print(newArr[i] + " ");
         }

    }
    public static boolean compare(double key, double average)
    {
        if(key >= average)
            return true;
        return false;
    }
}
Dung Lemanh
  • 74
  • 1
  • 12