java - Remove element from an array (code-specific) -


the user enters x-amount of integers, stored in array ('dataa'). average of data calculated, , element, furthest should removed ('outlier'). don't know how remove element , need know.

also, program removes 1 outlier, calculates new average , removes next outlier until there 1 element left.

public static scanner sc1 = new scanner(system.in); public static int datan = sc1.nextint();//number of data elements public static double[] dataa = new double[datan];//array storing elements  for(int index = 0; index<dataa.length; index++)//increments index     {         double lengtha = dataa.length;//length of array         double avg = sum/lengtha;//avg of elements          double outlier = dataa[0];//outlier         double index_outlier = 0;//index of outlier          double dif_in = math.abs(avg - dataa[index]);//difference of avg & element         double dif_out = math.abs(avg - outlier);//difference of avg & outlier          if(dif_in > dif_out)         {             outlier = dataa[index];             index_outlier = index;               } 

you can try swap outlier last element of array , continue array consider 1 less element. in case fine can use array like:

public static void main(string[] args) throws filenotfoundexception {         double[] dataarray = new double[] {1.5,2.5,3.5,4.5,7.5,8.5,2.5};         int arraysizetoconsider = dataarray.length;         double outlier;         int index_outlier;         double avg;         double diffinoutlierandavg;          while(arraysizetoconsider > 0) {             outlier = dataarray[0];             index_outlier = 0;             avg = computesum(dataarray,arraysizetoconsider) / (arraysizetoconsider);//avg of elements             diffinoutlierandavg = math.abs(avg - outlier);              // find outlier             for(int index = 0; index<arraysizetoconsider; index++)//increments index             {                 if(math.abs(avg - dataarray[index]) > diffinoutlierandavg) {                     outlier = dataarray[index];                     index_outlier = index;                 }             }             double temp = dataarray[arraysizetoconsider -1];             dataarray[arraysizetoconsider -1] = outlier;             dataarray[index_outlier] = temp;             arraysizetoconsider = arraysizetoconsider -1;             system.out.println("average: " + avg + " outlier: " + outlier + " index " + index_outlier + " array size consider: " +arraysizetoconsider);         }     }     private static double computesum(double[] array, int arraysizetoconsider) {         double sum = 0;         (int = 0; < arraysizetoconsider; i++) {             sum = sum + array[i];         }         return sum;     } 

and here output:

average: 4.357142857142857 outlier: 8.5 index 5 array size consider: 6 average: 3.6666666666666665 outlier: 7.5 index 4 array size consider: 5 average: 2.9 outlier: 4.5 index 3 array size consider: 4 average: 2.5 outlier: 1.5 index 0 array size consider: 3 average: 2.8333333333333335 outlier: 3.5 index 2 array size consider: 2 average: 2.5 outlier: 2.5 index 0 array size consider: 1 average: 2.5 outlier: 2.5 index 0 array size consider: 0

there more optimizations have left figure out.

hint: need compute sum every time find outlier ;)?


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -