From 497c7d0beabed19b8224fba790b029e7f2272131 Mon Sep 17 00:00:00 2001 From: Deathsbreed Date: Sat, 31 May 2014 23:49:00 -0500 Subject: [PATCH] Fixed Q1 calculations (though they could be simplified). --- src/MMMCalc.java | 50 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/src/MMMCalc.java b/src/MMMCalc.java index 3799035..38d8d29 100644 --- a/src/MMMCalc.java +++ b/src/MMMCalc.java @@ -136,30 +136,58 @@ public class MMMCalc { // FIXME: Q1 calculates incorrectly, it is supposed to be halfway between the first variable and the median variable. private static void calcQ1() { - int q1Pos = numArray.length / 4; - boolean exact; + int q1Pos; + // c stands for case... Cases correspond to their order in the following if statements (4 different cases). + int c; - if(numArray.length % 4 != 0) { - q1 = (numArray[q1Pos] + numArray[q1Pos-1]) / 2; - exact = false; + if(numArray.length % 2.0 == 0) { + q1Pos = (int)(numArray.length / 4); + if(numArray.length % 4.0 == 0) { + q1 = (numArray[q1Pos] + numArray[q1Pos-1]) / 2; + c = 1; + } else { + q1 = numArray[q1Pos]; + c = 2; + } } else { - q1 = numArray[q1Pos]; - exact = true; + if(Math.ceil(numArray.length / 2.0) % 2.0 == 0) { + q1Pos = (int)(Math.ceil(numArray.length / 2.0) / 2.0); + q1 = (numArray[q1Pos] + numArray[q1Pos+1]) / 2; + c = 3; + } else { + q1Pos = (int)Math.ceil(numArray.length / 4.0); + q1 = numArray[q1Pos]; + c = 4; + } } System.out.println("Q1: " + q1); if(verbose) { for(int i = 0; i < numArray.length; i++) { - if(!exact) { + if(c == 1) { if(i == q1Pos - 1) { - System.out.print(">>" + numArray[i] + " !" + q1 + "! "); - } else if( i == q1Pos) { + System.out.print(">>" + numArray[i] + " !" + q1 + "! "); + } else if(i == q1Pos) { System.out.print(numArray[i] + "<< "); } else { System.out.print(numArray[i] + " "); } - } else { + } else if(c == 2) { + if(i == q1Pos) { + System.out.print(">>" + numArray[i] + "<< "); + } else { + System.out.print(numArray[i] + " "); + } + } else if(c == 3) { + if(i == q1Pos) { + System.out.print(">>" + numArray[i] + " !" + q1 + "! "); + } else if(i == q1Pos + 1) { + System.out.print(numArray[i] + "<< "); + } else { + System.out.print(numArray[i] + " "); + } + } else if(c == 4) { if(i == q1Pos) { System.out.print(">>" + numArray[i] + "<< "); } else {