简体   繁体   English

计算数组中没有最大值和最小值的平均值

[英]Calculating average without highest and lowest values in an array

So I am trying to calculate the average and sum of an array, however I have to remove the highest and lowest values before I can calculate the avg/sum. 因此,我正在尝试计算数组的平均值和总和,但是在计算平均值/总和之前,必须删除最高和最低值。

I have code to fill the array with random doubles, and then find the highest and lowest value. 我有代码用随机双精度填充数组,然后找到最高和最低值。

What I am unsure about is if there is a way to subtract the highest and lowest values or if I have to copy the data from the array into a new one minus the highest and lowest values and then calc the avg/sum. 我不确定的是是否有一种方法可以减去最高和最低值,或者是否必须将数组中的数据复制到一个减去最高和最低值的新值中,然后计算平均值/总和。

This is what I have so far. 到目前为止,这就是我所拥有的。 Forgive me if this is somewhat obvious but I am stumped, and still in an intro to java course. 如果这很明显,请原谅我,但是我很困惑,并且仍然在Java课程入门中。

Here is my code so far. 到目前为止,这是我的代码。

double [] contestantOne = new double[8];

for (int index=0; index < contestantOne.length; index++) {
    contestantOne [index] = (double) (Math.random()*9) + 1;
}

for (int index=0; index < contestantOne.length; index++) {
    System.out.println( contestantOne [index] + "\n");
}

double contestantOneHigh; contestantOneHigh = contestantOne[0];

for (int index=1; index <contestantOne.length; index++) {    
    if (contestantOne[index] > contestantOneHigh)
        contestantOneHigh = contestantOne[index];
}

System.out.print("The highest value in your array is" 
               + " " + contestantOneHigh);
System.out.println();
System.out.println();

double contestantOneLow; contestantOneLow = contestantOne[0];
for (int index=1; index<contestantOne.length; index++) {   

    if (contestantOne [index] < contestantOneLow)
        contestantOneLow = contestantOne[index];
}    

System.out.print("The lowest value in your array is"
               + " " + contestantOneLow);
System.out.println();
System.out.println();

Calculate the sum like you normally would, but keep a variable each for the min and max, subtracting them out at the end: 像通常一样计算总和,但要为最小值和最大值分别保留一个变量,最后将其减去:

double min, max, sum;
min = max = sum = list[0];  // May want to add a check to make sure length > 1
for(int i = 1; i < list.length; i++) {
    double thisValue = list[i];
    sum += thisValue;
    min = Math.min(min, thisValue);
    max = Math.max(max, thisValue);
}
sum -= min + max;
double avg = sum / (list.length - 2);

Of course you may need to adjust the precise methods to suit the class you're using. 当然,您可能需要调整精确的方法以适合您所使用的类。

if you are going to calculate the average of an array of values, and you do not know if you will have zeros in it. 如果要计算值数组的平均值,并且不知道其中是否包含零。 I used: 我用了:

        int[] arrayDays = new int[] {monday,tuesday,wednesday,thursday,friday,saturday,sunday};
    int totalValue = 0;
    int divide = 0;

    for (int i=0;i<arrayDays.length;i++){
        if (arrayDays[i] != 0){
            totalValue = totalValue + arrayDays[i];
            divide = divide+1;
        }
    }
    Float average = 0f;
    if (divide!=0){
        average = Float.valueOf(totalValue / divide);
    }

Why don't you use a TreeSet instead of an array? 为什么不使用TreeSet而不是数组? This way, the elements are going to be sorted and you will be able to remove the higher and lowest elements with O(1) complexity instead of going through the whole array. 这样,将对元素进行排序,您将能够删除具有O(1)复杂度的较高和最低元素,而不用遍历整个数组。

You will only have to go through the collection to make the sum, and that's all. 您只需要遍历集合即可得出总和,仅此而已。 Your whole program will run in O(logn) + O(n) time. 您的整个程序将在O(logn)+ O(n)的时间运行。

public static void main(String[] args) {
    TreeSet<Double> contestantOne = new TreeSet<>();

    for (int index=0; index < 8; index++) {
        contestantOne.add((double) (Math.random()*9) + 1);
    }

    Iterator<Double> iterator = contestantOne.iterator();
    while(iterator.hasNext()){
        System.out.println(iterator.next());
    }

    double contestantOneHigh = contestantOne.pollLast();

    System.out.print("The highest value in your array is" 
                   + " " + contestantOneHigh);


    double contestantOneLow = contestantOne.pollFirst();

    System.out.println("The lowest value in your array is"
                   + " " + contestantOneLow);

    double sum = 0.0;
    iterator = contestantOne.iterator();

    while(iterator.hasNext()){
        sum += iterator.next();
    }

    System.out.println("The sum excluding first and last is: " + sum);
    System.out.println("The average excluding first and last is: " + sum/contestantOne.size());
}

traverse the array again checking if each element is equal to contestantOneLow/High, if it's not add to count, when you done divide this by contestantOne.length - 2 or just contestantOne.length. 再次遍历该数组,检查每个元素是否等于raceantOneLow / High,如果不将其相加,则将其除以CompetitionantOne.length-2或仅是raceantOne.length。 that should give you your avg. 那应该给你你的平均值。

I don't understand why you need to remove the high and low value from the array to get the average? 我不明白为什么您需要从数组中删除高值和低值以获得平均值? The average should be the sum of all values divided by the total number of values: 平均值应为所有值的总和除以值的总数:

double sum = 0;
for (int index=0; index < contestantOne.length; index++)
{
    //If you really need to remove highest and lowest value
    if (contestantOne[index] != contestantOneLow && contestantOne[index] != contestantOneHigh)
        sum += contestantOne[i];
}
double avg = sum / (double)(contestantOne.length - 2);

The problem with removing the values as done above, is if your array highs and lows aren't unique (array[10, 13, 15, 3, 15, 3, 6] 15 = High, 3 = Low) then your average will be incorrect when you calculate it because it will ignore both 15s and 3s. 如上所述删除值的问题是,如果您的数组的高点和低点不是唯一的(array [10、13、15、3、15、3、6] 15 =高,3 =低),则平均计算时不正确,因为它将忽略15s和3s。

I would suggest storing the index of high and index of low and using that instead. 我建议存储高索引和低索引,并改用它。

Arrays.sort(a);
find sum of array a
sum -= a[0] + a[a.length - 1]
return sum / (a.length - 2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM