简体   繁体   English

如何找到阵列的较大一半平均值?

[英]How do I find the larger half average of my array?

I'm working on a problem with an array where no matter the sequence of the array(ie how many numbers its pertains to and the order of them in the array. I need to find a way to get the upper half numbers(numbers past the median in terms of being greater not order) including the median value and average it. So far I am testing this but I am not good with arrays and need help我正在处理一个数组的问题,无论数组的顺序如何(即它涉及多少个数字以及它们在数组中的顺序。我需要找到一种方法来获取上半部分数字(过去的数字中位数更大而不是顺序)包括中位数和平均值。到目前为止,我正在测试这个,但我对 arrays 并不擅长,需要帮助

double[] tempArray = {92.0, 69.0, 35.0, 27.0, 9.0, 83.0, 89.0};]


            array = tempArray;
        }

        double avg = array[4];
        for(double i = 0; i<array.length; i++) {
            if(i >= avg)
            {
                System.out.println(+i);
double[] originalArray = {92.0, 69.0, 35.0, 27.0, 9.0, 83.0, 89.0};

double averageOfUpperHalfValues = findAverageOfUpperHalfValues(originalArray);
System.out.println(averageOfUpperHalfValues);

private double findAverageOfUpperHalfValues(double[] array) {
  double median = findMedian(array);
  double[] upperHalf = filterArrayByMinimumValue(inputs, minimum);
  double averageOfUpperHalf = findAverage(upperHalf);
  return averageOfUpperHalf;
}

private double findMedian(double[] array) {
  // Write some code to find the median value from an array.
}

private double[] filterArrayByMinimumValue(double[] array, double minimum) {
  // Write some code to return a smaller array of just the bigger values.
}

private double findAverage(double[] array) {
  // Write some code to find the average value from an array.
}

There's a basic structure for you.你有一个基本的结构。 The first method really just lists all the stuff that's going to happen.第一种方法实际上只是列出了所有将要发生的事情。 The last three methods contain the actual workings, split into separate activities so they are easy to read, test and reuse.最后三个方法包含实际的工作原理,分为单独的活动,因此它们易于阅读、测试和重用。 Made sure you do test them!!!确保你对它们进行测试!!!

Assuming you understand what the median is, findMedian(double[] array) should be fairly straigtforward.假设您了解中位数是什么, findMedian(double[] array)应该是相当直截了当的。 Hint: within this method, call another method to sort the array.提示:在这个方法中,调用另一个方法对数组进行排序。

filterArrayByMinimumValue(double[] array) has at least two possible approaches. filterArrayByMinimumValue(double[] array)至少有两种可能的方法。 1) trawl through the input array to count how many items will be in the filtered array, make an array of that size, then go back through the original array to find the values you want to keep and put them in the new array; 1)遍历输入数组以计算过滤后数组中有多少项,制作该大小的数组,然后 go 回到原始数组中找到要保留的值并将它们放入新数组中; return the new array.返回新数组。 2) make a collection type which doesn't have to have its size known in advance, go through the original array just once and copy all the values you want to keep over to your new collection, then convert the collection into an array of doubles and return it. 2)创建一个不必事先知道其大小的集合类型,go 通过原始数组只需一次,并将所有要保留的值复制到新集合中,然后将集合转换为双精度数组并返回它。

findAverage(double[] array) has an immediate point of concern. findAverage(double[] array)有一个直接的关注点。 There are three different types of 'average': mean, median and mode.存在三种不同类型的“平均值”:均值、中值和众数。 Clarify which of these you want (probably the mean, but know the differences and be certain).澄清你想要哪些(可能是平均值,但要知道差异并确定)。 Change the method name accordingly.相应地更改方法名称。 Write the code to do the job.编写代码来完成这项工作。 If I were you, I think I would write this method first because it looks easiest.如果我是你,我想我会先写这个方法,因为它看起来最简单。 No shame in starting off with a psychological boost:)从心理上的提升开始并不羞耻:)

If you have trouble with figuring out the code for any one of these three methods, come back to us with your best attempt and tell us what's puzzling you.如果您在找出这三种方法中任何一种方法的代码时遇到问题,请尽您最大的努力与我们联系,告诉我们您的困惑所在。

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

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