Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing an input file: output the median (Java)
    primarykey
    data
    text
    <p>I have been working on this for a while here is my code so far. I've been quite lost for sometime, because I do not know how to either change the number of items in an array <code>int m[] = new int[variable]</code> or remove the zeros/shift the array. The zeros are the problem, because I need to sort the array and find the median.</p> <p>Here's the assignment:</p> <blockquote> <h3>Task Description</h3> <p>Median plays an important role in the world of statistics. By definition, it is a value which divides an array into two equal parts. In this problem you are to determine the current median of some long integers.</p> <p>Suppose, we have five numbers {1,3,6,2,7}. In this case, 3 is the median as it has exactly two numbers on its each side. {1,2} and {6,7}.</p> <p>If there are even number of values like {1,3,6,2,7,8}, only one value cannot split this array into equal two parts, so we consider the average of the middle values {3,6}. Thus, the median will be (3+6)/2 = 4.5. In this problem, you have to print only the integer part, not the fractional. As a result, according to this problem, the median will be 4!</p> <h3>Program Input</h3> <p>The input file (c:\codewars\prob06.in) contains a number of integers X ( 0 &lt;= X &lt; 2^31 ) and total number of integers N is less than 10000. The input file will contain a ‘0” to signify the end of the list of integers. When you read in a ‘0’, your program must exit without calculating a new median with the number 0. </p> <pre><code>1 3 4 60 70 50 2 0 </code></pre> <h3>Program Output</h3> <p>As each number is read in, recalculate the median and display it to the screen. </p> <pre><code>1 2 3 3 4 27 4 </code></pre> </blockquote> <p>Here's my attempt so far:</p> <pre><code>import java.util.*; import java.io.*; public class Median { public static void main (String args[]) { String fileName = "textfile.txt"; //Labels scanner Scanner inputStream = null; int fileLen=0; int[] m = new int[1000]; try{ inputStream = new Scanner (new File (fileName)); LineNumberReader lnr = new LineNumberReader(new FileReader(new File(fileName))); lnr.skip(Long.MAX_VALUE); fileLen = lnr.getLineNumber(); } catch(Exception e) { System.out.println("Could not open the file named: " + fileName); System.exit(0); } int middle = 0; for (int counter=0;counter&lt;fileLen &amp;&amp; inputStream.hasNextInt();counter++){ int unChecked = inputStream.nextInt(); if (unChecked != 0) { m[counter]=unChecked; removeZeros(m); median(m, fileLen); //Arrays.sort(m); }else if (unChecked == 0){ counter+=fileLen; } } } public static int median(int m[],int fileLen) { int[] b = new int[fileLen]; System.arraycopy(m, 0, b, 0, b.length); Arrays.sort(b); System.out.println(b[1]); if (m.length % 2 == 0) { System.out.println((b[(b.length / 2) - 1] + b[b.length / 2]) / 2); return (b[(b.length / 2) - 1] + b[b.length / 2]) / 2; } else { System.out.println(b[b.length / 2]); return b[b.length / 2]; } } static void removeZeros(int m[]) { int counter2=0,counter4=0; for(int counter=0;counter&lt;m.length;counter++){ if (m[counter]!=0){ counter2++; System.out.println(m[counter]); } } int d[] = new int [counter2]; for(int counter=0;counter&lt;m.length;counter++){ if (m[counter]==0){ d[counter4] = m[counter]; counter4++; } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload