gpt4 book ai didi

java - 查找二维数组的标准差

转载 作者:太空宇宙 更新时间:2023-11-04 07:22:34 25 4
gpt4 key购买 nike

我正在努力寻找我的作业的标准差,我已经做了其他所有事情,但我只是在最后一点上挣扎。简而言之,我的作业问题是:我收到了一组学生作业结果的文本文件(必须是任意数量的)。给出的唯一数据是学生姓名、粉丝和他们的成绩,您需要计算他们的平均成绩和标准差。输出应如下所示:

Student Name     FAN      Part 1 Part 2 Part 3 Part 4 Mark    Grade
Adam Adamson adam0001 85.4 79.8 82.4 86.1 82.77% DN
Bethany Bright brig0001 89.7 85.6 84.2 82.9 84.92% DN
Cameron Carlson carl0001 55.45 49.82 60.4 42.27 50.23% P
David Dawson daws0001 72.6 78.49 80.2 65.88 74.46% CR
Evelyn Ellis elli0001 50.2 35.88 48.41 58.37 46.57% FA
Frances Fitz fitz0001 78.9 75.67 82.48 79.1 78.38% DN
Greg Gregson greg0001 24.3 32.88 29.72 28.4 30.05% F
Harriett Hope hope0001 52.2 58.93 61.5 63.44 60.12% P
Ivan Indigo indi0001 88.4 91.23 90.05 92.46 91.08% HD
Jessica Jones jone0001 82.33 89.74 81.3 84.85 85.84% HD
Average 67.948 67.804 70.066 68.377 68.44% CR
StdDev 19.4442

我知道我需要使用的方程,我知道我需要使用哪些数字,但我只是不知道如何获得它们。我将向您展示我需要做什么,希望您能进一步理解我的代码:

the total of the averages divided by 10 = 68.442

sum = (82.77-68.442)^2 + (84.92-68.442)^2 + (50.23-68.442)^2 + ... + (85.84-68.442)^2
StdDev = Math.sqrt(sum/10)

stdDev = 19.4442

我为此使用了 3 个类,我的主类主题管理,一个学生类,其中包含一个包含学生姓名和粉丝的数组(两者都与我的问题无关,但我认为我最好只是设置场景),最后是 StudentsMarks 类,其中包含下面显示的我的 StdDec 方法:

public static double StdDev() throws IOException
{
String [][] marks = StudentMarks.StudentMarks();

double mean = 0, average = 0, stdDev = 0;
int row = 0;
for(row = 0; row < marks.length; row++)
{ //ROW,COL
double score1 = Double.parseDouble(marks[row][2]); //parsing the data from the array into a double
double score2 = Double.parseDouble(marks[row][3]);
double score3 = Double.parseDouble(marks[row][4]);
double score4 = Double.parseDouble(marks[row][5]);

average = score1*0.1 + score2*0.4 + score3*0.2 + score4*0.3;

mean = average/row;
stdDev = Math.sqrt(Math.pow(average - mean,2)/row);
}
return stdDev;
}

最佳答案

你需要 2 个循环,因为你需要从每一行中减去平均值。

double[] sum = new double[marks.length];

for(row = 0; row < marks.length; row++)
{ //ROW,COL
double score1 = Double.parseDouble(marks[row][2]); //parsing the data from the array into a double
double score2 = Double.parseDouble(marks[row][3]);
double score3 = Double.parseDouble(marks[row][4]);
double score4 = Double.parseDouble(marks[row][5]);

sum[row] = score1*0.1 + score2*0.4 + score3*0.2 + score4*0.3;
average += sum[row];
}
average = average/sum.length; //1
for(row = 0; row < sum.length; row++)
{
mean += Math.pow(Math.abs(sum[row] - average),2);
}
mean = mean/marks.length;//2
stdDev = Math.sqrt(mean);

关于java - 查找二维数组的标准差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19077442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com