如何将值保存到数组?

时间:2015-05-08 02:55:21

标签: c++ arrays

仍在尝试编写一个可用于确定学期结束时成绩的C ++程序。对于每个学生,由1到60之间的整数标识,必须保留四个考试成绩。此外,必须计算两个最终成绩平均值。一年级平均值只是所有四个等级的平均值。二级平均值通过如下权重来计算四个等级:第一等级的权重为0.2,第二等级的权重为0.3,第三等级的权重为0.3,第四等级的权重为0.2;计算如下: 0.2 *等级1 + 0.3 *等级2 + 0.3 *等级3 + 0.2 *等级4 使用此信息,您将构建一个60 X 7二维数组,其中第一列用于学生编号,后四列用于成绩,最后两列用于计算的最终成绩。程序的输出应该是已完成数组中数据的显示。

您的程序应打印成绩矩阵(不需要表格网格),包括平均值。然后它应该计算该类的两个平均值的平均值和标准偏差并打印结果。

这是我到目前为止所做的,但我不知道如何在不将各个点放入程序的情况下计算标准偏差。帮助

#include <iostream>
#include <math.h>
using namespace std;

int grade_Calc(int sg[][5]); //initiates function grade_Calc

int main()
{
int student_grades[5][5] =
{
    {1, 100, 100, 100, 100}, //inputs an array of students 1-5's grades
    {2, 100, 0, 100, 0},
    {3, 82, 94, 73, 86},
    {4, 64, 74, 84, 94},
    {5, 94, 84, 74, 64},
    };

grade_Calc(student_grades); //passes array student_grades into function grade_Calc
return 0;
}


int grade_Calc(int sg[][5])
{
int sum_for_avg = 0; //initializes sum_for_avg (adds up students scores for taking the mean)
double weighted_grade = 0.0; //initializes weighted_grade (uses formula 0.2 * grade1 + 0.3 * grade2+ 0.3* grade3 + 0.2 * grade4)
double simple_avg = 0.0; //initializes simple_avg (arithmetic mean)
double class_mean1 = 0.0;
double class_mean1_sum = 0.0;
double class_mean2 = 0.0;
double class_mean2_sum = 0.0;
double standardDeviation1 = 0.0;
double standardDeviation2 = 0.0;


cout << "Stdnt" << "\t" << "Grd1" << "\t" << "Grd2" << "\t" << "Grd3" << "\t" << "Grd4" << "\t" << "Avg1" << "\t" << "Avg2" << endl; //outputs column headings
for (int r = 0; r < 5; r++) //goes through array rows
{
    for (int c = 0; c < 5; c++) //goes through array columns
    {
        cout << sg[r][c] << "\t\t"; //outputs array elements with 2 tabs between each element

        if (c != 0) //ensures that the student number isn't included in the calculations
        {
            sum_for_avg += sg[r][c]; //adds up students scores for taking the mean

            if ((c == 1) || (c == 4)) //if-else that calculates weighted_grade (uses formula 0.2 * grade1 + 0.3 * grade2+ 0.3* grade3 + 0.2 * grade4)
            {
                weighted_grade += (0.2 * sg[r][c]);
            }
            else
            {
                weighted_grade += (0.3 * sg[r][c]);
            }
        }
    }

    simple_avg = (sum_for_avg / 4.0); //calculates simple_avg (arithmetic mean)

    cout << "\t" << simple_avg << "\t" << weighted_grade; //outputs simple_avg in Avg1 column and weighted_grade in Avg2 column

    class_mean1_sum += simple_avg;
    class_mean1 = class_mean1_sum/5;
    standardDeviation1 = sqrt(pow(100-class_mean1,2) +
                             pow(50-class_mean1,2) +
                             pow(83.75-class_mean1,2) +
                             pow(79-class_mean1,2) +
                             pow(79-class_mean1,2)/ 5.0);

    class_mean2_sum += weighted_grade;
    class_mean2 = class_mean2_sum/5;
    standardDeviation2 = sqrt(pow(100-class_mean1,2) +
                              pow(50-class_mean1,2) +
                              pow(83.7-class_mean1,2) +
                              pow(79-class_mean1,2) +
                              pow(79-class_mean1,2)/ 5.0);

    sum_for_avg = 0;
    weighted_grade = 0.0;
    cout << endl;
    }

cout << endl;
cout << "The class average for Avg1 is " << class_mean1;

cout << endl;
cout << "The standard deviation for Avg1 is " << standardDeviation1;

cout << endl;
cout << "The class average for Avg 2 is " << class_mean2;

cout << endl;
cout << "The standard deviation for Avg2 is " << standardDeviation2 << endl;

return 0;

}

0 个答案:

没有答案