How to calculate the median of an array?

后端 未结 14 1668
北海茫月
北海茫月 2020-12-05 06:10

I\'m trying to calculate the total, mean and median of an array thats populated by input received by a textfield. I\'ve managed to work out the total and the mean, I just ca

相关标签:
14条回答
  • 2020-12-05 07:13
    Arrays.sort(numArray);
    return (numArray[size/2] + numArray[(size-1)/2]) / 2;
    
    0 讨论(0)
  • 2020-12-05 07:14
    public int[] data={31, 29, 47, 48, 23, 30, 21
            , 40, 23, 39, 47, 47, 42, 44, 23, 26, 44, 32, 20, 40};
    
    public double median()
        {
            Arrays.sort(this.data);
            double result=0;
            int size=this.data.length;
    
    
            if(size%2==1)
            {
                result=data[((size-1)/2)+1];
                System.out.println(" uneven size : "+result);
            }
            else
            { 
                int middle_pair_first_index =(size-1)/2;
                result=(data[middle_pair_first_index+1]+data[middle_pair_first_index])/2;
                System.out.println(" Even size : "+result);
            }
    
            return result;
        }
    
    0 讨论(0)
提交回复
热议问题