d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Exercise > 32.44 Fg Tip For Best Answer ;)
Add Reply New Topic New Poll
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
May 4 2013 02:45pm
Hey :)

The task is to read in a file containing N doubles (readings from an experiment), then compute the average A1 of all values.
After that, calculate the average A2 of all values excluding the furthest value away from A1.
After that, calculate the average A3 of all values excluding the furthest and second furthest value away from from A2.
...
After that, calculate the average AN of all values excluding the (N-1) furthest away values from A(N-1).

As far as I understood the task, it is okay to disregard special cases like a double appearing several times in the list, or several doubles in the list being equally far away from the last calculated average value.

Here's my take on it, it works for my simple testing file containing 3 8 9 0. I feel my code is overly complicated, is there a better/simpler solution without using array methods (my course did not introduce these yet)?



This post was edited by tt_toby on May 4 2013 02:53pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 4 2013 03:17pm
i didn't read your code.

i'd approach the problem like this:
1) read in the array
2) sort the array
3) get the sum
4) have a pointer at the start and end of the array
5) each value farthest from Ax must be on the edge. so subtract the appropriate values from the sum and move the pointers.

This post was edited by carteblanche on May 4 2013 03:17pm
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
May 4 2013 03:20pm
Sorting should not be required, although I know how to sort, the exercise is in a chapter before sorting methods are discussed.
There are pointers in java?!
Member
Posts: 4,841
Joined: Jan 16 2008
Gold: 0.00
May 4 2013 05:37pm
there's a line missing in the code I posted:
Insert
delta=Math.abs(avg-readings[i]);
after line 41.

here's another version which checks for multiple occurences of furthest.



This post was edited by tt_toby on May 4 2013 05:42pm
Member
Posts: 1,241
Joined: Jun 25 2011
Gold: Locked
May 5 2013 11:54am
Hey, here's a recursive and cleaner version of your code, can explain any part if needed, just lmk:

exercise.java
Code
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;


public class exercise {
public static void main(String[] args) throws FileNotFoundException {
 File fp = new File("fourtyseven6.in");
 Scanner s = new Scanner(fp);
 ArrayList<Double> theDoubles = new ArrayList<Double>();
 while(s.hasNextDouble())
  theDoubles.add(new Double(s.nextDouble()));
 compute(theDoubles, 1);
}

public static void compute(ArrayList<Double> theDoubles, int i) {
 double delta = 0, avg = 0;
 Double furthestAway = null;
 for(Double d : theDoubles)
  avg += d.doubleValue();
 avg /= theDoubles.size();
 System.out.println("Average A" + i + ": " + avg);
 for(Double d : theDoubles) {
  if(Math.abs(d.doubleValue() - avg) > delta) {
   delta = Math.abs(d.doubleValue() - avg);
   furthestAway = d;
  }
 }
 if(furthestAway != null) {
  theDoubles.remove(furthestAway);
  compute(theDoubles, i + 1);
 }
}
}


also
Quote
There are pointers in java?!


There's no C-style pointers in Java, but you have references which is roughly the same thing (in my here code, furthestAway would be a reference a Double object).

This post was edited by m0hawk on May 5 2013 12:02pm
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
May 5 2013 12:58pm
In essence, everything in Java is a pointer -- you just can't change a pointer's value outside of the scope in which it was created. Java passes arguments strictly by reference and not by value.

This post was edited by irimi on May 5 2013 12:59pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll