我对整个排序数组作了算术平均值,但现在我想对数组的第一个排序一半和第二个排序一半进行算术平均值。
例如:我的数组是:77、99、44、55、22、88、11、00、66、33。 我的代码排在首位。
程序的结果是:00 11 22 33 44 55 66 77 88 99。
现在我想对上半部分取平均值: 00 11 22 33 44并打印。
然后我想对下半部分取平均值: 55 66 77 88 99并打印。
public class Array {
private double[] a;
private int NrElmts;
public Array(int max)
{ a = new double[max];
NrElmts = 0;
}
public void elements(double value)
{ a[NrElmts] = value;
NrElmts++;
}
public void print()
{ for(int j=0; j<NrElmts; j++)
System.out.print(a[j] + " ");
System.out.println("");
}
public void selectionSort()
{
int out, in, min;
for(out=0; out< NrElmts -1; out++)
{ min = out;
for(in=out+1; in< NrElmts; in++)
if(a[in] < a[min] )
min = in;
invertPositions(out, min); }
}
private void invertPositions(int one, int two)
{ double temp = a[one];
a[one] = a[two];
a[two] = temp;
}
public void mean()
{
int i;
double sum = 0;
for(i = 0; i < NrElmts; i++) {
sum+=a[i];}
double medie = sum/NrElmts;
System.out.format("Mean is: %.1f", mean);
System.out.println("");
}
}
答案 0 :(得分:1)
试试这个
public void firstHalfMean(){
int i;
double sum = 0;
int numberOFElements = NrElmts/2;
for (i = 0; i < NrElmts/2; i++) { // take sum only till half.
sum += a[i];
}
double mean = sum / numberOFElements; // sum/half the elements
System.out.format("Mean is: %.1f", mean);
System.out.println("");
}
public void secondHalfMean(){
int i;
double sum = 0;
int numberOFElements = NrElmts % 2 == 0 ? NrElmts/2 : NrElmts/2 + 1; // If odd, this second array will contain one more element.
for (i = NrElmts/2; i < NrElmts; i++) { // take sum for the next half
sum += a[i];
}
double mean = sum / numberOFElements; // sum/half elements (half + 1) in case of odd length.
System.out.format("Mean is: %.1f", mean);
System.out.println("");
}
答案 1 :(得分:0)
由于您已经有方法对整个数组进行平均,因此您所需要做的就是找到数组的中间位置,然后从该点开始运行。 在您的示例中:NrElmts为10,因此将NrElmnts除以2,这样您就可以得到1到5的均值,然后得到6到10的均值,两者均为5。
考虑一下数组中元素数量奇数的情况,无论是第一个数组还是第二个数组,您都想怎么做?让我知道是否也需要帮助。
步骤: 1)创建一个新变量,比如说a1到NrElmts / 2,并使用平均函数从1到a1 2)从a1 + 1转到NrElmnts
让我知道您是否需要任何帮助。
答案 2 :(得分:0)
要计算9、2和7的平均值,必须首先将它们全部加起来,等于18,然后除以多少-18/3等于6。
虽然,您将不得不考虑出现奇数列表的可能性-如果元素数量奇数,例如1、2、3,则3的中间点为1.5-并且如果要通过索引进行迭代,变量会将中间点计为1。所以这有点棘手,不确定您要做什么。
请考虑以下代码-它确实可以满足您的要求,但是带有奇数列表大小,它只会除以十进制值
LinkedList<Integer> numbers = new LinkedList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.add(40);
int size = numbers.size();
int iterativeHalf = size / 2;
float meanHalf = (float) size / 2;
float lowerMean = 0;
float upperMean = 0;
for (int i = 0; i < size; i++) {
int realRef = i + 1;
Integer value = numbers.get(i);
if (realRef > iterativeHalf) { //Should be calculating upper mean
if (upperMean == 0) { //if lowerMean is just a running total, not divided yet to get the mean
System.out.println("the lower mean for numbers is " + lowerMean + " / " + meanHalf);
lowerMean = (lowerMean) / meanHalf; //add last value + divide to set it to the mean
}
System.out.println("upper mean = " + upperMean + " + " + value + " = " + (upperMean + value));
upperMean = upperMean + value; //keep the upper values up total going
} else {
System.out.println("lower mean = " + lowerMean + " + " + value + " = " + (lowerMean + value));
lowerMean = lowerMean + value; //keep adding the lower halfs values up
}
}
//When it breaks, must divide upperMean by size to get mean
System.out.println("the upper mean for numbers is " + upperMean + " / " + meanHalf);
upperMean = (upperMean) / meanHalf;
System.out.println(" ");
System.out.println("FINAL lower mean = " + lowerMean);
System.out.println("FINAL upper mean = " + upperMean);
输出为:
lower mean = 0.0 + 10 = 10.0
lower mean = 10.0 + 20 = 30.0
the lower mean for numbers is 30.0 / 2.0
upper mean = 0.0 + 30 = 30.0
upper mean = 30.0 + 40 = 70.0
the upper mean for numbers is 70.0 / 2.0
FINAL upper mean = 35.0
FINAL lower mean = 15.0
对于[10、20、30、40],这将产生上面显示的输出,但基本上是(10 + 20)/ 2作为下平均值,而(30 + 40)/ 2是上平均值。
< br />
对于[10,20,30,40,50]将产生(10 + 20)/ 2.5的较低平均值,而(30 + 40 + 50)/2.5的较高平均值
答案 3 :(得分:0)
仅取数组一半的总和。如果数组大小为奇数,则在后半部分或前半部分再增加一个元素。
public void firstHalfMean(){
int i;
double sum = 0;
int numberOFElements = NrElmts/2;
for (i = 0; i < NrElmts/2; i++) { // take sum only till half.
sum += a[i];
}
double mean = sum / numberOFElements; // sum/half the elements
System.out.format("Mean is: %.1f", mean);
System.out.println("");
}
public void secondHalfMean(){
int i;
double sum = 0;
int numberOFElements = NrElmts % 2 == 0 ? NrElmts/2 : NrElmts/2 + 1; // If odd, this second array will contain one more element.
for (i = NrElmts/2; i < NrElmts; i++) { // take sum for the next half
sum += a[i];
}
double mean = sum / numberOFElements; // sum/half elements (half + 1) in case of odd length.
System.out.format("Mean is: %.1f", mean);
System.out.println("");
}